java-过滤器Filter_多个Filter的执行顺序

http://www.cnblogs.com/HigginCui/p/5772514.html

【Filter链】

*在一个web应用中,可以开发编写多个Filter,这些Filter组合起来称为一个Filter链。

*web服务器根据Filter在web.xml中的注册顺序,决定先调用哪个Filter,当第一个Filter的doFilter方法被调用时,web服务器会创建一个代表Filter链的FilterChain对象传递给该方法,在doFilter方法中,开发人员如果调用了FilterChain对象的doFilter方法,则web服务器会检查FilterChain对象中是否还有filter,如果有,则调用第二个filter,如果没有,则调用目标资源。

 

【工程截图】

设计了两个Filter和一个Servlet,访问Servlet时,查看Filter的执行顺序。

java-过滤器Filter_多个Filter的执行顺序_第1张图片

【web.xml】

复制代码
xml version="1.0" encoding="UTF-8"?>
<web-app xmlns: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>FilterDemo01display-name> <welcome-file-list> <welcome-file>index.jspwelcome-file> welcome-file-list>  <filter> <filter-name>FilterTest01filter-name> <filter-class>com.Higgin.web.filter.FilterTest01filter-class> filter> <filter-mapping> <filter-name>FilterTest01filter-name> <url-pattern>/*url-pattern>  filter-mapping> <filter> <filter-name>FilterTest02filter-name> <filter-class>com.Higgin.web.filter.FilterTest02filter-class> filter> <filter-mapping> <filter-name>FilterTest02filter-name> <url-pattern>/*url-pattern>  filter-mapping> web-app>
复制代码

 

【FilterTest01.java】

复制代码
package com.Higgin.web.filter;

import java.io.IOException;

import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class FilterTest01 implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  System.out.println("FilterTest01 执行前---");  chain.doFilter(request, response);//让目标资源执行,即:放行  System.out.println("FilterTest01 执行后---"); } @Override public void init(FilterConfig arg0) throws ServletException { } @Override public void destroy() { } }
复制代码

【FilterTest02.java】

复制代码
package com.Higgin.web.filter;

import java.io.IOException;

import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class FilterTest02 implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  System.out.println("FilterTest02 执行前---"); chain.doFilter(request, response); //放行 System.out.println("FilterTest02 执行后---"); } @Override public void init(FilterConfig arg0) throws ServletException { } @Override public void destroy() { } }
复制代码

 

【ServletTest01.java】

复制代码
package com.Higgin.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServletTest01 */ @WebServlet("/ServletTest01") public class ServletTest01 extends HttpServlet { private static final long serialVersionUID = 1L; public ServletTest01() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.getWriter().write("中国 加油!China Come on!"); System.out.println("执行ServletTest01---"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
复制代码

【运行结果】

在浏览器中输入:http://localhost:8080/FilterDemo01/ServletTest01

查看控制台输出结果

java-过滤器Filter_多个Filter的执行顺序_第2张图片

 

自己可以尝试分别注释FilterTest01和FilterTest02中的chain.doFilter方法,用Junit查看具体的执行过程。

你可能感兴趣的:(java-过滤器Filter_多个Filter的执行顺序)