-1

I am building a control filter for my entityManager in my app, but I am getting this error:

Caused by: java.lang.IllegalArgumentException: Invalid <url-pattern> [Faces Servlet] in filter mapping
    at org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:3025)
    at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:2971)
    at org.apache.catalina.startup.ContextConfig.configureContext(ContextConfig.java:1289)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1169)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:775)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:301)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5051)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    ... 27 more

when I define @WebFilter("Faces Servlet") in my class JPAFilter. I changed to @WebFilter(".xhtml"), but the entityManager creates a lot of connections in the db.

My JPAFilter class:

@WebFilter("Faces Servlet")
public class JPAFilter implements Filter {

    private EntityManagerFactory entityManagerFactory;
    private String persistence_unit_name = "comex-pu";

    public void destroy() {
        this.entityManagerFactory.close();
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        EntityManager entityManager = this.entityManagerFactory.createEntityManager();
        request.setAttribute("entityManager", entityManager);
        entityManager.getTransaction().begin();
        chain.doFilter(request, response);

        try {
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            entityManager.getTransaction().rollback();
        } finally {
            entityManager.close();
        }
    }

    public void init(FilterConfig fConfig) throws ServletException {
        this.entityManagerFactory = Persistence.createEntityManagerFactory(this.persistence_unit_name);
    }
}

My Faces Servlet in web.xml:

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nicholas
  • 11
  • 1

1 Answers1

2

Your mistake is here:

@WebFilter("Faces Servlet")

You specified Faces Servlet as default value of the annotation (i.e. you did not assign it to any specific attribute name). If you look up the meaning of the default value in the javadoc of @WebFilter, then you read this:

value
The URL patterns to which the filter applies

So, the default value of the annotation is interpreted as an URL pattern. This totally explains the exception you got:

Invalid <url-pattern> [Faces Servlet] in filter mapping

You're basically trying to specify the servlet name Faces Servlet as an URL pattern. You need to specify the servlet name via servletNames attribute instead.

@WebFilter(servletNames = { "Faces Servlet" })

That should solve the exception.


However, this won't necessarily solve your XY-problem of "too many connections", because this change will let your filter still behave exactly the same as when you configured it as @WebFilter("*.xhtml"). This is because the servlet name "Faces Servlet" is according to web.xml mapped to .... yes, also *.xhtml. So the net effect is that there is no difference whatsoever.

Most probably you actually wanted to skip JSF resources in your filter (CSS/JS/image assets). They namely also match the *.xhtml URL pattern. You need to solve this differently. Just let your filter skip URLs starting with context path plus ResourceHandler.RESOURCE_IDENTIFIER. Put this code in the very beginning of your doFilter() method.

if (request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
    chain.doFilter(request, response);
    return;
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555