Yeah, sorry, there isn't an exclude pattern in the servlet spec. Well I'm stupid and stubborn (the worst combination I guess), so of course I had to figure out some way to overcome this limitation. Even though you can basically do everything with include patterns and by examining the url in the filter code itself, the thing is that exclude url pattern would be very useful in various situations. Consider url authorization implemented with a filter for example. You might only have one or a few URLs, like the login page, that you want to be accessible to everyone, and for security reasons, you want say "check authorization to all URLs except for this one". If you just used include, there's a always a chance that you forget to add that new directory you just created into your filter mapping. If you included everything and then bypassed the one URL in you filter code, you'd essentially have to hard-code the URL in. However, you could take the bypass idea a bit further and implement a simple exclude filter that would bypass all the subsequent filters in your filter chain by forwarding the request. Your whole doFilter() method in your BypassFilter could be just two lines of code (or even one):
HttpServletRequest httpRequest = (HttpServletRequest)request;
// consider if you'd like to add request parameters into the
// forwarded request (+ httpRequest.getQueryString() )
request.getRequestDispatcher( httpRequest.getRequestURI().substring(httpRequest.getContextPath().length()) ).forward(request, response);
Bypassing the filter chain also works well for loading static resources, such as images etc. for which you likely won't want to do any extra processing in filters.
And btw, Resin supports excludes in filter mapping.
Posted by thoughts at September 28, 2004 02:22 AM | TrackBackYes, you bypass the rest of the filter chain, but your forward() will make you enter this filter again and again, if web.xml still has a /* url-pattern. Your server will probably shutdown.
Am I right or any further explanation? Thanks.
Posted by: Donovan at January 7, 2005 02:03 PMIn Resin you can use a regular expression in the filter mapping and thus exclude a particular URL from entering the filter chain.
http://www.caucho.com/resin-3.0/config/webapp.xtp#filter-mapping
Posted by: Anoop at July 21, 2005 05:53 PMYeah. I know that - in many ways, Resin is better than Tomcat. And besides, I like the name :)
Posted by: Alphageek at September 13, 2005 10:19 PMHi Friend,
I am using url- pattern *.do but it is not even bypassing filter class.
Please explain me why is it so?
Posted by: Dharmesh at October 10, 2005 10:53 PMI had the same idea when I came across the same limitation. However I didt not realize that the resource URI was relative to the context root. This works great with JBoss404+Tomcat5.5.x :)
Thanks !
Posted by: Pål Kristiansen at July 21, 2006 02:16 AM