Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Configuring Filters in Tomcat 5

 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to setup a filter in my webapps web.xml file but it only seems to work with a specific filename and not the entire directory as needed.

Here's my filter element from the web.xml file.
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>net.domain.auth.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
***********************************************************
**** this works fine with 'menu.jsp' but if I change it to '*.jsp'
**** it kills the app.
***********************************************************
<url-pattern>/management/menu.jsp</url-pattern>
</filter-mapping>

Do I have the wrong syntax?
<url-pattern>/management/*.jsp</url-pattern>

Thanks,
Dave
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're mixing two different URL-patterns. In the Servlet Spec section SRV.11.2:

In the web application deployment descriptor, the following syntax is used
to define mappings:

� A string beginning with a �/� character and ending with a �/*� postfix
is used for path mapping.
� A string beginning with a �*.� prefix is used as an extension mapping.
� A string containing only the �/� character indicates the "default"
servlet of the application. In this case the servlet path is the request
URI minus the context path and the path info is null.
� All other strings are used for exact matches only.

The last point is the kicker. If it doesn't match any of the three patterns, it defaults to 'exact match'. So your pattern of "/management/*.jsp" would try to match a file named "*.jsp" in the /management directory. Which is not what you wanted, and it probably would break before attempting the match.

Can you use "/management/*" ?

This would have the effect of protecting all JSPs under the management directory (which is what you seem to be going for with your pattern.

Note, that would also protect *.gif and *.html (and everything else you might access that starts with /management).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic