Servlet 3.0 @ WebFilter how to control the execution sequence of multiple filters, @ webfilterfilter
Previously, we controlled the execution sequence of multiple filters through web. in xml, the filter position is controlled. If the filter is placed above, it is executed first. The following "User Login check filter" is executed first than "interface LOG filter ".
<! -- User Logon detection filter -->
<Filter> <filter-name> UserLoginFilter </filter-name>
<Filter-class> net. tfgzs. demo. filter. UserLoginFilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> UserLoginFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
<! -- Interface LOG filter --> <filter-name> ApiLog </filter-name> <filter-class> net. tfgzs. demo. filter. apiLog </filter-class> </filter> <filter-mapping> <filter-name> ApiLog </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
However, when we use the @ WebFilter annotation, we find that the annotation does not provide parameters that can control the execution sequence.
@ WebFilter attributes
| Attribute name |
Type |
Description |
| FilterName |
String |
Specify the name attribute of the filter, which is equivalent to <filter-name> |
| Value |
String [] |
This attribute is equivalent to the urlPatterns attribute. However, they should not be used at the same time. |
| UrlPatterns |
String [] |
Specifies the URL matching mode for a set of filters. It is equivalent to the <url-pattern> label. |
| ServletNames |
String [] |
Specify the Servlet to which the filter applies. The value is the value of the name attribute in @ WebServlet, or the value of <servlet-name> in web. xml. |
| DispatcherTypes |
DispatcherType |
Specify the forwarding mode of the filter. Specific values include async, ERROR, FORWARD, INCLUDE, and REQUEST. |
| InitParams |
WebInitParam [] |
Specify a set of filter initialization parameters, which is equivalent to the <init-param> label. |
| AsyncSupported |
Boolean |
Whether the declared filter supports asynchronous operation mode is equivalent to the <async-supported> label. |
| Description |
String |
The description of the filter, which is equivalent to the <description> tag. |
| DisplayName |
String |
The display name of the filter, usually used with tools, is equivalent to the <display-name> label. |
It is found through practice that if you want to control the execution sequence of filer, you canControl the file name of the filterTo control
For example:
The UserLoginFilter. java and ApiLog. java files contain "User Login check filter" and "interface LOG filter", becauseBefore the first letter of A row of UIn this case, the "interface LOG filter" is first executed and then the "user login check filter" is executed. Therefore, we need to change the names of the two files
Filter0_UserLogin.java
Filterpolicapilog.java
In this way, you can first execute "User Login check filter" and then "interface LOG filter"
This article from the "take off Studio" blog, please be sure to keep this source http://www.cnblogs.com/tfgzs/p/4571137.html