一、FilterChain 过滤器链
多个Filter对同一个资源进行拦截就可以形成过滤器链,同一个资源是指的匹配路径一致
多个Filter过滤器是根据配置的顺序来执行,谁在前,先执行谁
二、Filter的生命周期
1、生命周期流程
和servlet 生命周期类似:实例化-》初始化->服务-》销毁
Filter的生命周期
服务器启动时,会创建Filter对象, 并调用init方法,只调用一次
当访问资源的时,路径与Filter的拦截路径匹配,会执行Filter的doFilter方法
当服务器关闭时,会调用Filter 的destroy方法进行销毁
2、代码展示
package com.heihei; 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 MyFilter implements Filter{ @Override public void destroy() { System.out.println("Filter destroy "); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("拦截开始"); //放行 chain.doFilter(request, response); System.out.println("结束拦截,资源过滤后,回来了"); } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("Filter init "); } }
三、FilterConfig对象说明
Filter初始化方法init上的一个参数 ,就是FilterConfig对象
public interface Filter { void init(FilterConfig var1) throws ServletException; void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException; void destroy(); }
MyFilterConfig
package com.heihei; 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 MyFilterConfig implements Filter{ private FilterConfig config; @Override public void init(FilterConfig config) throws ServletException { this.config = config; } @Override public void doFilter(ServletRequest req, ServletResponse rsp, FilterChain chain) throws IOException, ServletException { //通过FilterConfig对象获取配置文件中的初始化信息 String coding = config.getInitParameter("encoding"); System.out.println(coding); req.setCharacterEncoding(coding); chain.doFilter(req, rsp); } @Override public void destroy() { // TODO Auto-generated method stub } }
ServletDemo
package com.heihei; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("ServletDemo"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doPost(req, resp); } }
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"> <filter> <filter-name>MyFilterConfig</filter-name> <filter-class>com.heihei.MyFilterConfig</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>MyFilterConfig</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>ServletDemo</servlet-name> <servlet-class>com.heihei.ServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemo</servlet-name> <url-pattern>/ServletDemo</url-pattern> </servlet-mapping> </web-app>
四、Filter配置
1、匹配规则
在web.xm文件中,<url-pattern>标签的匹配规则: 完全匹配 以"/demo",不包含通配符 目录匹配 以"/"开头,以"*"结束 扩展名匹配: *.xxxx,不能写成 /*.xxx ,比如 *.do
2、servlet-name 标签
也可以直接通过配置<servlet-name>标签,来关联到servlet对应的servlet-mapping配置,从而达到拦截的目的 <filter-mapping> <filter-name>MyFilterConfig</filter-name> <servlet-name>ServletDemo</servlet-name> </filter-mapping>
3、dispatcher 标签
<dispatcher>标签,默认是request请求,可以写多个dispatcher标签,比如: <filter-mapping> <filter-name>MyFilterConfig</filter-name> **** <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> REQUEST:默认值,只过滤客户端请求和重定向 FORWARD:只过滤转发请求