Java Filter的執行順序 – JAVA編程語言程序開發技術文章

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.














Request path Filter Sequence
/Recipes/          1, 5
/Recipes/ 1, 5, 2
/Recipes/Modify/ 1, 5, 4
/ 5
/Recipes/Add/ 1, 3, 5