title: Servlet filter url mapping
body: <p>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:</p>
  
  <pre><code>  &lt;filter&gt;
          &lt;filter-name&gt;My filter&lt;/filter-name&gt;
          &lt;filter-class&gt;com.myApp.myFilter&lt;/filter-class&gt;        
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;My filter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/sign_in.xhtml&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>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)</p>
  
  <pre><code>&lt;url-pattern&gt;/&lt;/url-pattern&gt;
  </code></pre>
  
  <p>maps to the default servlet, and an empty string maps to the root.  Here's the relevant section from the spec:</p>
  
  <p>"The empty string ("") is a special URL pattern that exactly maps to the
  application's context root, i.e., requests of the form <a href="http://host:port/" rel="noreferrer">http://host:port/</a>/. In this case the path info is ’/’ and the servlet path and context path is
  empty string (““)."</p>
  
  <p>However, both of the following url patterns cause Tomcat to throw an exception.</p>
  
  <pre><code>&lt;url-pattern&gt;&lt;/url-pattern&gt;
  &lt;url-pattern&gt;""&lt;/url-pattern&gt;
  </code></pre>
  
  <p>I would really appreciate it if someone could shed some light on this. Thank You.</p>
  
  <p>Andrew</p>