I stumbled upon a bug in my web application that had me scratching my head (and eventually pulling my hair) for a while before I found out what was going on.
Basica
When the container recives a request, it first finds all the filter mappings with a <url-pattern>
that matches the request URI. This becomes the first set of filters in the filter chain.Next it finds all the filter mappings with a <servlet-name>
that matches the request URI. This becomes the second set of filters in the filter chain.In both the sets the filters are executed in the order in which they are declared in the Deployment descriptor(D.D.)
According to the specs
The order the container uses in building the chain of filters to be applied for a particular request URI is as follows:
- First, the
<url-pattern>
matching filter mappings in the same order that these elements appear in the deployment descriptor.- Next, the
<servlet-name>
matching filter mappings in the same order that these elements appear in the deployment descriptor.
Additionally, you can define the order in which Filters are applied. This may be accomplished by adding the following lines in your web.xml:
<absolute-ordering>
<name>encodingFilter</name>
<name>SpringFormMethodFilter</name>
</absolute-ordering>
Check this for further information.