Web. XML filter configuration and execution sequence concepts

Source: Internet
Author: User

First filter

@Override
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
Boolean B1 = false;
if (B1 = True) {
To continue this request, if there are multiple filters, go to the next filter
Chain.dofilter (request, response);
} else {
Redirect this request
((httpservletresponse) response). Sendredirect ("/xxxx");
}
System.out.println ("urlfilter_1 doFilter ...");

}


-----------------------------------------------------------------------------------

A second filter

@Override
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
Boolean B2 = false;
if (B2 = True) {
To continue this request, if there are multiple filters, go to the next filter
Chain.dofilter (request, response);
} else {
Redirect this request
((httpservletresponse) response). Sendredirect ("/xxxx");
}
System.out.println ("urlfilter_1 doFilter ...");

}

-----------------------------------------------------------------------------------

A third filter

@Override
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
Boolean B3 = false;
if (B3 = True) {
To continue this request, if there are multiple filters, go to the next filter
Chain.dofilter (request, response);
} else {
Redirect this request
((httpservletresponse) response). Sendredirect ("/xxxx");
}
System.out.println ("urlfilter_1 doFilter ...");

}

--------------------------------------------------------------------------------------------

The Web,xml is configured as follows

<filter>
<filter-name>url_1</filter-name>
<filter-class>com.boya.filters.UrlFilter_1</filter-class>
</filter>

<filter>
<filter-name>url_3</filter-name>
<filter-class>com.boya.filters.UrlFilter_3</filter-class>
</filter>
<filter>
<filter-name>url_2</filter-name>
<filter-class>com.boya.filters.UrlFilter_2</filter-class>
</filter>


<filter-mapping>
<filter-name>url_3</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>url_1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>url_2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

--------------------------------------------------------------------------------------
Execution results are

Urlfilter_1 initialization ...
Urlfilter_3 initialization ...
Urlfilter_2 initialization ...
2013-9-29 13:38:40 Org.apache.coyote.AbstractProtocol Start
Info: Starting Protocolhandler ["http-bio-8080"]
2013-9-29 13:38:40 Org.apache.coyote.AbstractProtocol Start
Info: Starting Protocolhandler ["ajp-bio-8009"]
2013-9-29 13:38:40 Org.apache.catalina.startup.Catalina Start
Info: Server Startup in 596 ms
Urlfilter_2 DoFilter ...
Urlfilter_1 DoFilter ...
Urlfilter_3 DoFilter ...

-------------------------------------------------------------------------------------

Analysis results:

1.
Chain.dofilter (request, response);
In order to continue this request, if there is the next filter in the next filter, do not jump directly to the requested link.

2.
The order in which the filters are executed and the XML
<filter-mapping>
<filter-name>xxx</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The order of the XML configuration is performed from bottom to top.

Web. XML filter configuration and execution sequence concepts

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.