Display the filter servlet URL

How can a filter map to the root of a url? I am using Tomcat 7.0.2 and deploying the application as ROOT.war. Welcome page - sign_in.xhtml. I would like to run the filter whenever the client sends a request for the root of the site (i.e. only the domain name) or when the client requests sign_in.xhtml. Here is what I still have:

<filter> <filter-name>My filter</filter-name> <filter-class>com.myApp.myFilter</filter-class> </filter> <filter-mapping> <filter-name>My filter</filter-name> <url-pattern>/sign_in.xhtml</url-pattern> </filter-mapping> 

Requests for sign_in.xhtml directly, successfully invoke the filter, but I'm not sure how to get root requests to invoke the filter. According to Servlet specification (version 3.0)

 <url-pattern>/</url-pattern> 

displays the servlet by default, and the empty string maps to the root. Here is the relevant section from the specification:

"Empty line (" ") is a special URL pattern that exactly matches ie requests of the form http: // host: port / /. In this case, the path information is /, and the servlet path and context path are an empty string ( "").

However, both of the following url patterns cause Tomcat to throw an exception.

 <url-pattern></url-pattern> <url-pattern>""</url-pattern> 

I would really appreciate it if someone could shed light on this. Thanks.

Andrew

+7
java servlets servlet-filters url-pattern
source share
1 answer

<url-pattern>/</url-pattern> should work for queries in the root. Have you tried

If your intent is to filter all requests more, then you should use <url-pattern>/*</url-pattern> .


Refresh . To exclude one and the other, I tested the url-pattern of / in Tomcat 7 (using both web.xml and @WebFilter(urlPatterns={"/"}) ) and it really didn't work as expected. url-pattern of / works, as expected, on Tomcat 6 (Servlet 2.5), as well as Glassfish v3 (Servlet 3.0). I suspect a bug in Tomcat 7, so I reported issue 49914 about this.

+7
source share

All Articles