过滤器链

---------------siwuxie095

   

   

   

   

   

   

   

   

一个 Web 应用中可以一次编写多个过滤器,这些过滤器的组合

称之为 过滤器链

   

在过滤器链中,过滤器的执行顺序依据过滤器在部署描述符 web .xml 中

注册的顺序

   

   

过滤器链体现了一种职责链模式,是设计模式中的一种

   

   

职责链模式:

   

能够使多个对象,都有机会处理请求,它将多个对象连成一个链条,

并沿着这个链条传递请求,直到有一个对象最终处理它为止

   

   

职责链模式能够避免请求的发送者和接受者之间的耦合关系

   

   

对于过滤器而言,请求传递给具体的 Web 资源之前,都会经过

过滤器的处理,在 Web 资源处理完该请求之后-返回响应给客户

端之前,响应信息也会被过滤器处理一遍

   

   

   

   

一个简单的过滤器链实例:

   

访问指定资源之前打印若干日志,访问指定资源之后再打印若干日志

   

   

点击选择工程名,右键->New->Filter,包名为:com.siwuxie095.filter,

类名为:FilterOne、FilterTwo

   

   

   

FilterOne.java:

   

package com.siwuxie095.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 FilterOne implements Filter {

   

 

public FilterOne() {

System.out.println("===== FilterOne 构造方法 =====");

}

 

 

public void init(FilterConfig fConfig) throws ServletException {

System.out.println("===== FilterOne 初始化方法 =====");

String initParam=fConfig.getInitParameter("param");

System.out.println("param = "+initParam);

}

   

 

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

System.out.println("===== 开始执行FilterOne doFilter方法 =====");

chain.doFilter(request, response);

System.out.println("===== 结束执行FilterOne doFilter方法 =====");

}

   

 

public void destroy() {

System.out.println("===== FilterOne 销毁方法 =====");

}

   

}

   

   

   

FilterTwo.java:

   

package com.siwuxie095.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 FilterTwo implements Filter {

   

 

public FilterTwo() {

System.out.println("===== FilterTwo 构造方法 =====");

}

   

 

public void init(FilterConfig fConfig) throws ServletException {

System.out.println("===== FilterTwo 初始化方法 =====");

}

   

 

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

System.out.println("===== 开始执行FilterTwo doFilter方法 =====");

chain.doFilter(request, response);

System.out.println("===== 结束执行FilterTwo doFilter方法 =====");

}

 

 

public void destroy() {

System.out.println("===== FilterTwo 销毁方法 =====");

}

   

}

   

   

   

在部署描述符 web.xml 中注册 filter:

   

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

<display-name>MyServlet</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

 

<filter>

<filter-name>FilterOne</filter-name>

<filter-class>com.siwuxie095.filter.FilterOne</filter-class>

<init-param>

<param-name>param</param-name>

<param-value>siwuxie095</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>FilterOne</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

   

<filter>

<filter-name>FilterTwo</filter-name>

<filter-class>com.siwuxie095.filter.FilterTwo</filter-class>

</filter>

<filter-mapping>

<filter-name>FilterTwo</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

</web-app>

   

   

部署描述符 web.xml 在 WEB-INF 目录下,如果没有,手动创建即可

   

点击选择工程名,右键->Java EE Tools->Generate Deployment Descriptor Stub

   

   

   

启动 Tomcat 时,过滤器的执行:

   

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/6716489.html