Servlet3.0之前Filter过滤的顺序是由用户在web.xml中配置的顺序决定的,如下会先执行encodingFilter,再执行filter1.
- <filter>
- <display-name>encodingFilter</display-name>
- <filter-name>EncodingFilter</filter-name>
- <filter-class>com.saber.domain.EncodingFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <display-name>filter1</display-name>
- <filter-name>filter1</filter-name>
- <filter-class>com.saber.domain.Filter1</filter-class>
- </filter>
- <filter-mapping>
- <filter-name><span style="font-family: Arial, Helvetica, sans-serif;">filter1</span><span style="font-family: Arial, Helvetica, sans-serif;"></filter-name></span>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
在3.0之后新增@WebFilter注解,当使用注解配置多个Filter时,用户无法控制其执行顺序,此时Filter过滤的顺序是按照Filter的类名来控制的,按自然排序的规则。