How to map a filter for welcome-file in web.xml fo

2020-03-26 07:04发布

问题:

I have created a Filter to check the Cookies with request object and forward the request accordingly thanks to this and this question..Now I want to map this Filter for just the welcome-file as I have declared it in web.xml.
Say I have welcome-file as index.html then I have to map the Filter for both www.example.com/ and www.example.com/index.html but not for anything else such as www.example.com/foo/* or www.example.com/* is not allowed, I mean NOT anything except welcome-file request. I am using Apache Tomcat 7.0.22 container.

回答1:

Just add these to your web.xml

<filter>
  <filter-name>myFilter</filter-name>
  <filter-class>com.examples.myFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>myFilter</filter-name>
  <url-pattern>/index.html</url-pattern>
</filter-mapping>