Java servlet filter chain order 

Joined:
04/09/2007
Posts:
727

November 23, 2010 20:41:28    Last update: January 14, 2011 16:15:20
The servlet API allows servlet filters to be inserted into the processing cycle to form a Filter Chain. How is a chain defined, and what is the ordering of filters in the chain?

The servlet filter chain is formed by defining multiple filters for the same servlet or URL pattern in web.xml. The order in which the filters are invoked is the same order as <filter-mapping>s appear in the web.xml file.

For example, if this is defined in web.xml:
<!-- Filter mapping -->
<filter-mapping>
        <filter-name>servletFilter2</filter-name>
        <servlet-name>filterdemo</servlet-name>
</filter-mapping>

<filter-mapping>
        <filter-name>servletFilter</filter-name>
        <servlet-name>filterdemo</servlet-name>
</filter-mapping>

then servletFilter2 will be applied before servletFilter.

Actually, it's more accurate to imagine these filters as layers or wraps instead of chains. In the above example, servletFilter2 wraps servletFilter.
Share |
| Comment  | Tags
 
Easy email testing with http://www.ximailstop.com