【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的执行顺序。
【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>FilterDemo01</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 过滤顺序:谁的写在上面,谁先被过滤 --> <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-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-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
查看控制台输出结果
自己可以尝试分别注释FilterTest01和FilterTest02中的chain.doFilter方法,用Junit查看具体的执行过程。
时间: 2024-10-16 11:50:29