Servlet 3.0 @webfilter How to control the execution order of multiple filter

Source: Internet
Author: User

Before we control multiple filter execution order is controlled by the location of the control filter in Web. XML, the above will be executed before the following, "User login Check filter" will be compared to "interface log Filter" to perform first

<!--User Login 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> <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>

But when we use @webfilter annotations, we find that the annotations do not provide parameters that control the order of execution.

properties of the @WebFilter
Property name type Description
FilterName String Specify the Name property of the filter, equivalent to <filter-name>
Value String[] This property is equivalent to the Urlpatterns property. But both should not be used at the same time.
Urlpatterns String[] Specifies the URL-matching pattern for a set of filters. Equivalent to <url-pattern> tag.
Servletnames String[] Specifies which servlets the filter will be applied to. The value is the value of the name attribute in the @WebServlet, or the value of <servlet-name> in Web. Xml.
Dispatchertypes Dispatchertype Specifies the forwarding mode of the filter. Specific values include: ASYNC, ERROR, FORWARD, include, REQUEST.
InitParams Webinitparam[] Specifies a set of filter initialization parameters, equivalent to the <init-param> tag.
asyncsupported Boolean Declares whether the filter supports asynchronous operation mode, equivalent to the <async-supported> tag.
Description String The description information of the filter is equivalent to the <description> tag.
DisplayName String The filter's display name, usually used with the tool, is equivalent to the <display-name> tag.

Through practice, it is found that if you want to control the order of filer execution, you can control the file name of the filter

Like what:

Userloginfilter.java and Apilog.java These two files are "User login check Filter" and "Interface log Filter", because these two files before the first letter a row U , resulting in each execution is executed first " The interface log Filter "then executes the user login check filter", so we now modify the names of two files respectively for

Filter0_userlogin.java

Filter1_apilog.java

This allows you to perform a "User login check Filter" and then "interface Log Filter"

This article is from the "Ascendas Studio " blog, make sure to keep this source http://www.cnblogs.com/tfgzs/p/4571137.html

Servlet 3.0 @webfilter How to control the execution order of multiple filter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.