IMPORTANT: The Container's rules for ordering filters: 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. <filter-name> BeerRequest1 </filter-name> <url-pattern> *. do </url-pattern> </filter-mapping> <filter-name> BeerRequest2 </filter-name> <servlet-name> AdviceServlet </servlet-name> </filter -mapping> Write
Down the sequence in which the filters will be executed for each request path. Assume Filter1-Filter5 have been properly declared.
When more than one filter is mapped to a given resource, the Container uses the following rules:
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.
To put it bluntly, there are two:
1) First, execute the filter with the url-pattern tag, and then execute the filter with the servlet-name tag. The two tags are written as follows:
<Filter-mapping>
<Filter-name> BeerRequest1 </filter-name>
<Url-pattern> *. do </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-name> BeerRequest2 </filter-name>
<Servlet-name> AdviceServlet </servlet-name>
</Filter-mapping>
The former takes priority.
2) If the url-pattern or servlet-name is the same, it will be executed in the declared order in web. xml.
The following is an example:
[Html]
<Filter-mapping>
<Filter-name> Filter1 </filter-name>
<Url-pattern>/Recipes/* </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-name> Filter2 </filter-name>
<Servlet-name>/Recipes/HopsList. do </servlet-name>
</Filter-mapping>
<Filter-mapping>
<Filter-name> Filter3 </filter-name>
<Url-pattern>/Recipes/Add/* </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-name> Filter4 </filter-name>
<Servlet-name>/Recipes/Modify/ModRecipes. do </servlet-name>
</Filter-mapping>
<Filter-mapping>
<Filter-name> Filter5 </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
Request path Filter Sequence
/Recipes/HopsReport. do 1, 5
/Recipes/HopsList. do 1, 5, 2
/Recipes/Modify/ModRecipes. do 1, 5, 4
/HopsList. do 5
/Recipes/Add/AddRecipes. do 1, 3, 5