Thursday, July 31, 2008

Servlet filter doesn't work, or does it ?

It has been a while I found the time to blog. Here is a little entry.

I've often hear that a Servlet filter was not working while trying to filter some Java resource. Usually this is because the filter is defined like:


<filter-mapping>
<filter-name>My Broken Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


and the filtered content is accessed through request dispatching.

This look correct, but this is not. In the 2.4 version of the Servlet spec you can now filter resources accessed directly from the request, through an include request dispatcher, through a forward request dispatch or through the error mechanism of the spec.
By default, if nothing is specified, the filter will only apply to direct requests. This is the case here and this is what confuses people.

If you want to filter request coming from an include request dispatcher you need to add: <dispatcher>INCLUDE</dispatcher>

Here are few examples:
The following will only filter direct requests:

<filter-mapping>
<filter-name>My Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


The following will only filter direct requests too:

<filter-mapping>
<filter-name>My Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>


The following will only filter direct requests and inclusions:

<filter-mapping>
<filter-name>My Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>


The following will only filter inclusions:

<filter-mapping>
<filter-name>My Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>


The following will filter all:

<filter-mapping>
<filter-name>My Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>


Want more ? Check the servlet spec, "SRV.6.2.5 Filters and the RequestDispatcher"

5 comments:

Mike Kaufman said...

And in case anyone's wondering: in Servlet 2.3 (which first introduced filters), filters are only applied to direct requests (i.e. not invoked when forwarding, including, or error-ing).

Anonymous said...

Can anyone recommend the top performing Network Management program for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: [url=http://www.n-able.com] N-able N-central remote pc access
[/url] ? What is your best take in cost vs performance among those three? I need a good advice please... Thanks in advance!

Anonymous said...

Good Day!!! blog.thomas.heute.name is one of the most outstanding innovative websites of its kind. I enjoy reading it every day. All the best.

Anonymous said...

Ihr habt eine schoene Webseite hier, und vielciht schaut Ihr euch auchmal meine an, ok Sex im Internet ist nicht jedermans Sache, aber eben meine erste Homepage. Danke und macht weiter so! http://www.sexcamamateure.net/sexgays.php

Anonymous said...

The author of blog.thomas.heute.name has written an excellent article. You have made your point and there is not much to argue about. It is like the following universal truth that you can not argue with: Infamy is FAR more fun than fame. Thanks for the info.