When a filter intercepts a request to a response URL, it executes the code before Chain.dofilter () in the DoFilter () method, and then executes the next filter or Servelt. Immediately following the execution of the Code after Chain.dofilter ().
The order of execution for two filters:
Filter One:
PackageCom.rskd_yswb.lib.filter;Importjavax.servlet.*;Importjava.io.IOException; Public classHttprequestandresponsefilterImplementsFilter {PrivateFilterconfig Filterconfig; Public voiddestroy () {} Public voidDoFilter (ServletRequest req, Servletresponse resp, filterchain chain)throwsservletexception, IOException {resp.setcharacterencoding ( This. Filterconfig.getinitparameter ("Encoding")); System.out.println ("Httprequestandresponsefilterbefore"); Chain.dofilter (req, resp); System.out.println ("Httprequestandresponsefilterafter"); } Public voidInit (filterconfig config)throwsservletexception { This. Filterconfig =config; }}
Filter Two:
PackageCom.rskd_yswb.lib.filter;Importjavax.servlet.*;Importjava.io.IOException; Public classTest2filterImplementsFilter { Public voiddestroy () {} Public voidDoFilter (ServletRequest req, Servletresponse resp, filterchain chain)throwsservletexception, IOException {System.out.println ("Test2filterbefore"); Chain.dofilter (req, resp); System.out.println ("Test2filterafter"); } Public voidInit (filterconfig config)throwsservletexception {}}
Servlet to execute:
PackageCom.rskd_yswb.servelt;Importcom.rskd_yswb.javabean.db.ConsumerEntity;Importcom.rskd_yswb.lib.db.DBConnection;Importcom.rskd_yswb.lib.db.GenerateSqlStatement;ImportCom.rskd_yswb.lib.freemarker.FreeMarker;Importfreemarker.template.Template;Importfreemarker.template.TemplateException;ImportOrg.apache.commons.dbutils.QueryRunner;ImportOrg.apache.commons.dbutils.handlers.BeanMapHandler;ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger;ImportJavax.servlet.annotation.WebServlet;Importjava.io.IOException;ImportJava.io.Writer;Importjava.sql.Connection;ImportJava.util.HashMap;ImportJava.util.Map; Public classIndexservletextendsJavax.servlet.http.HttpServlet {Private StaticLogger Logger = Logmanager.getlogger (indexservlet.class); protected voidDoPost (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)throwsjavax.servlet.ServletException, IOException {doget (request, response); } protected voidDoget (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)throwsjavax.servlet.ServletException, IOException {String SQL=NewGeneratesqlstatement (). Select ("*"). From ("Consumer"). get (); Try{Map<string, consumerentity> consumer =NewQueryrunner (). Query (Dbconnection.getconnection (), SQL,NewBeanmaphandler<string, Consumerentity> (consumerentity.class, "username")); } Catch(Exception e) {logger.debug ("Indexservlet Error:" +e.getmessage ()); } Map<string, object> root =NewHashmap<string, object>(); Try{freemarker.getfreemarker (). GetTemplate ("INDEX.FTL"). Process (root, response.getwriter ()); System.out.println ("In Request Processing"); } Catch(templateexception e) {logger.debug ("Indexservlet Error:" +e.getmessage ()); } }}
The Web. XML configuration reads as follows:
<Web-app> <Filter> <Filter-name>Httprequestandresponsefilter</Filter-name> <Filter-class>Com.rskd_yswb.lib.filter.HttpRequestAndResponseFilter</Filter-class> <Init-param> <Param-name>Encoding</Param-name> <Param-value>Utf8</Param-value> </Init-param> </Filter> <Filter> <Filter-name>Test2filter</Filter-name> <Filter-class>Com.rskd_yswb.lib.filter.Test2Filter</Filter-class> </Filter> <filter-mapping> <Filter-name>Httprequestandresponsefilter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping> <filter-mapping> <Filter-name>Test2filter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping> <servlet> <Servlet-name>Indexservlet</Servlet-name> <Servlet-class>Com.rskd_yswb.servelt.IndexServlet</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Indexservlet</Servlet-name> <Url-pattern>/index</Url-pattern> </servlet-mapping> <servlet-mapping> <Servlet-name>Indexservlet</Servlet-name> <Url-pattern></Url-pattern> </servlet-mapping></Web-app>
The execution order of the filter is like the order of the configuration in Web. XML, and the Http://localhost:8080/index console will print the following in the browser input:
Httprequestandresponsefilterbeforetest2filterbefore Request Processing in Test2filterafterhttprequestandresponsefilterafter
Order of execution of code before and after Chain.dofilter () in the filter filter in Java