java 过滤器Filter中chain.doFilter()之前和之后代码的执行顺序

过滤器拦截到响应url的请求后会先执行doFilter()方法中chain.doFilter()之前的代码,然后执行下一个过滤器或者servelt。紧接着执行chain.doFilter()之后的代码。

一下为两个过滤器的执行顺序:

过滤器一:

package com.rskd_yswb.lib.filter;

import javax.servlet.*;
import java.io.IOException; public class HttpRequestAndResponseFilter implements Filter {
private FilterConfig filterConfig; public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
resp.setCharacterEncoding(this.filterConfig.getInitParameter("encoding"));
System.out.println("HttpRequestAndResponseFilterBefore");
chain.doFilter(req, resp);
System.out.println("HttpRequestAndResponseFilterAfter");
} public void init(FilterConfig config) throws ServletException {
this.filterConfig = config;
} }

过滤器二:

package com.rskd_yswb.lib.filter;

import javax.servlet.*;
import java.io.IOException; public class Test2Filter implements Filter {
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("Test2FilterBefore");
chain.doFilter(req, resp);
System.out.println("Test2FilterAfter");
} public void init(FilterConfig config) throws ServletException { } }

要执行的servlet:

package com.rskd_yswb.servelt;

import com.rskd_yswb.javabean.db.ConsumerEntity;
import com.rskd_yswb.lib.db.DBConnection;
import com.rskd_yswb.lib.db.GenerateSqlStatement;
import com.rskd_yswb.lib.freemarker.FreeMarker;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanMapHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map; public class IndexServlet extends javax.servlet.http.HttpServlet {
private static Logger logger = LogManager.getLogger(IndexServlet.class); protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doGet(request, response);
} protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
String sql = new GenerateSqlStatement().select("*").from("consumer").get();
try {
Map<String, ConsumerEntity> consumer = new QueryRunner().query(DBConnection.getConnection(), sql, new BeanMapHandler<String, ConsumerEntity>(ConsumerEntity.class, "username"));
} catch (Exception e) {
logger.debug("IndexServlet Error:" + e.getMessage());
}
Map<String, Object> root = new HashMap<String, Object>();
try {
FreeMarker.getFreemarker().getTemplate("index.ftl").process(root, response.getWriter());
System.out.println("请求处理中");
} catch (TemplateException e) {
logger.debug("IndexServlet Error:" + e.getMessage());
}
}
}

web.xml配置内容如下:

<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>

filter的执行顺序如同web.xml中的配置的顺序,在浏览器输入http://localhost:8080/index 控制台将打印如下内容:

HttpRequestAndResponseFilterBefore
Test2FilterBefore
请求处理中
Test2FilterAfter
HttpRequestAndResponseFilterAfter
上一篇:Java eclipse下 Ant build.xml实例详解 附完整项目源码


下一篇:最菜的小鸟(mkdir -pv)