多个Filter 的执行顺序

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.

说白了就两条:

1)先执行带有url-pattern标签的filter,再执行带有servlet-name标签的filter。两种标签的书写方式如下:

<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>

前者优先执行。

2)如果同为url-pattern或servlet-name,则会按照在web.xml中的声明顺序执行。

下面举个例子:

[html]  view plain copy
  1. <filter-mapping>  
  2. <filter-name>Filter1</filter-name>  
  3. <url-pattern>/Recipes/*</url-pattern>  
  4. </filter-mapping>  
  5. <filter-mapping>  
  6. <filter-name>Filter2</filter-name>  
  7. <servlet-name>/Recipes/HopsList.do</servlet-name>  
  8. </filter-mapping>  
  9. <filter-mapping>  
  10. <filter-name>Filter3</filter-name>  
  11. <url-pattern>/Recipes/Add/*</url-pattern>  
  12. </filter-mapping>  
  13. <filter-mapping>  
  14. <filter-name>Filter4</filter-name>  
  15. <servlet-name>/Recipes/Modify/ModRecipes.do</servlet-name>  
  16. </filter-mapping>  
  17. <filter-mapping>  
  18. <filter-name>Filter5</filter-name>  
  19. <url-pattern>/*</url-pattern>  
  20. </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


转载地址:http://blog.csdn.net/xxfigo/article/details/7991395

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值