servlet3.0 @webfilter 过滤顺序

时间:2021-08-26 03:21:42

Servlet3.0之前Filter过滤的顺序是由用户在web.xml中配置的顺序决定的,如下会先执行encodingFilter,再执行filter1.

  1. <filter>
  2. <display-name>encodingFilter</display-name>
  3. <filter-name>EncodingFilter</filter-name>
  4. <filter-class>com.saber.domain.EncodingFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>encodingFilter</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>
  10. <filter>
  11. <display-name>filter1</display-name>
  12. <filter-name>filter1</filter-name>
  13. <filter-class>com.saber.domain.Filter1</filter-class>
  14. </filter>
  15. <filter-mapping>
  16. <filter-name><span style="font-family: Arial, Helvetica, sans-serif;">filter1</span><span style="font-family: Arial, Helvetica, sans-serif;"></filter-name></span>
  17. <url-pattern>/*</url-pattern>
  18. </filter-mapping>

在3.0之后新增@WebFilter注解,当使用注解配置多个Filter时,用户无法控制其执行顺序,此时Filter过滤的顺序是按照Filter的类名来控制的,按自然排序的规则。