8

How can a filter be mapped to the root of a URL? I'm using Tomcat 7.0.2 and deploying an application as ROOT.war. The welcome page is sign_in.xhtml. I would like to run a filter whenever the client sends a request for the root of the site (i.e. the domain name only), or when the the client requests sign_in.xhtml. Here is what I have so far:

  <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 requests for the root to invoke the filter. According to the Servlet spec (version 3.0)

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

maps to the default servlet, and an empty string maps to the root. Here's the relevant section from the spec:

"The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port//. In this case the path info is ’/’ and the servlet path and context path is 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 some light on this. Thank You.

Andrew

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
J. Andrew Laughlin
  • 1,926
  • 3
  • 21
  • 33

1 Answers1

8

The <url-pattern>/</url-pattern> should work for requests on the root. Did you try it?

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


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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for looking into this BalusC. I'm intending to force redirection if the user is required to change their password. A filter seems like the proper solution. I guess I'll have to wait for 7.0.3 or come up with something different. Any ideas? – J. Andrew Laughlin Sep 10 '10 at 21:09
  • A filter listening on `/*` which takes action only if the request URI is empty. – BalusC Sep 10 '10 at 21:36
  • I'll give it a go and then update the url-patten once the Tomcat bug is fixed. – J. Andrew Laughlin Sep 10 '10 at 22:08
  • @Balus - I have the exact problem now - I am using Tomcat7 and wants to forward all calls with the request URL empty (www.mysite.com) to servlet. But if the URI is not empty - it shouldn't direct it to the servlet. I am using tomcat 7.9.16 - which way shall I do it? – Dejell Jul 06 '11 at 21:22
  • @Odelya: Map a filter on `/*` and determine `HttpServletRequest#getRequestURI()` yourself. If the URI is empty, dispatch to `/` by `RequestDispatcher#forward()`, else just continue the filter chain. – BalusC Jul 06 '11 at 21:24
  • @Balus - how does it influence performance to go on my filter? – Dejell Jul 07 '11 at 06:37
  • 1
    @Odelya: That extra `if-else` check? It's fully negligible. – BalusC Jul 07 '11 at 17:31
  • 1
    @BalusC </` should work for requests on the root.>> No it does not, for Tomcat 6 (Servlet 2.5). Any update re this topic? – Joseph Victor Zammit Jul 20 '12 at 15:19