04_ Filter filter_03_ multiple filter execution order

Source: Internet
Author: User

"Filter Chain"

* In a Web application, you can develop and write multiple filter combinations called a filter chain.

The *web server determines which filter to call first, based on the order in which the filter is registered in Web. XML, and when the first filter's Dofilter method is called, the Web server creates a Filterchain object representing the filter chain to the method, in the Dofilter party Method, if the developer invokes the Dofilter method of the Filterchain object, the Web server checks to see if there is a filter in the Filterchain object, and if so, invokes the second filter and, if not, the target resource.

Engineering

Two filter and one servlet were designed to see the execution order of the filter when accessing the servlet.

"Web. Xml"

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">  <Display-name>FilterDemo01</Display-name>  <welcome-file-list>    <Welcome-file>index.jsp</Welcome-file>  </welcome-file-list>     <!--  filter Order: Who's written on top, who was first filtered-    -  <Filter>    <Filter-name>FilterTest01</Filter-name>    <Filter-class>Com. Higgin.web.filter.FilterTest01</Filter-class>  </Filter>  <filter-mapping>    <Filter-name>FilterTest01</Filter-name>    <Url-pattern>/*</Url-pattern> <!--Filter All -  </filter-mapping>    <Filter>    <Filter-name>FilterTest02</Filter-name>    <Filter-class>Com. Higgin.web.filter.FilterTest02</Filter-class>  </Filter>  <filter-mapping>    <Filter-name>FilterTest02</Filter-name>    <Url-pattern>/*</Url-pattern> <!--Filter All -  </filter-mapping>    </Web-app>

"Filtertest01.java"

 Packagecom. Higgin.web.filter;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classFilterTest01Implementsfilter{@Override Public void doFilter(servletrequest request, Servletresponse response, Filterchain 
    
     chain)
    throwsIOException, servletexception { System.out.println ( "FilterTest01---before Execution"); Chain.dofilter (request, response); //let the target resource execute, namely: Release System.out.println("FilterTest01---after Execution"); } @Override Public voidInit (Filterconfig arg0)throwsservletexception {} @Override Public voiddestroy () {}}

"Filtertest02.java"

 Packagecom. Higgin.web.filter;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classFilterTest02Implementsfilter{@Override Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {  System.out.println( "FilterTest02---before Execution"); Chain.dofilter (request, response); Release System.out.println("FilterTest02---after Execution"); } @Override Public voidInit (Filterconfig arg0)throwsservletexception {} @Override Public voiddestroy () {}}

"Servlettest01.java"

 Packagecom. Higgin.web.servlet;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet Implementation class ServletTest01*/@WebServlet ("/servlettest01") Public classServletTest01extendsHttpServlet {Private Static Final LongSerialversionuid = 1L;  PublicServletTest01 () {Super(); }    protected void Doget(HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//response.getwriter (). Write ("China refueling! China Come on! ");        System.out.println ("Performing ServletTest01---"); }    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request,response); }}

"Run Results"

In the browser, type:http://localhost:8080/FilterDemo01/ServletTest01

viewing console output Results

You can try to annotate the Chain.dofilter method in FilterTest01 and FilterTest02 separately, and use JUnit to see the specific execution process.

04_ Filter filter_03_ multiple filter execution order

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.