IMPORTANT: The Container’s rules for ordering filters:
When more than one filter is mapped to a given resource, the Container uses the following rules:
1) ALL filters with matching URL patterns are located first. This is NOT the same as the URL mapping rules the Container uses to choose the “winner” when a client makes a request for a resource, because ALL filters that match will be placed in the chain!! Filters with matching URL patterns are placed in the chain in the order in which they are declared in the DD.
2) Once all filters with matching URLs are placed in the chain, the Container does the same thing with filters that have a matching <servlet-name> in the DD.
specifically i couldnt understand the 2nd point. please explain it with a simple example ?
Let's explain it with an example where we have a Servlet (e.g. FilterServlet) with the following mapping:
We can now attach filters on the basis of the url-pattern, for example: http://localhost:8080/WebAppRoot/filter/*, or we can attach a filter on the Servlet name (FilterServlet). If we have the following filters defined in our web.xml:
If we have deployed our web app and type in the following URL: http://localhost:8080/WebAppRoot/filter/FilterServlet
We will see the following execution order (where the last Sysem.out is from the FilterServlet itself)
You can see that the filters with a matching Servlet-name are invoked later than the ones on the basis of the URL-pattern.