过滤器七:过滤链

目录

一:过滤链简介

二:过滤链案例

三:第一个需要注意的问题:chain.dofilter()书写位置    与    请求和响应过程中执行的关系

补:控制过滤链中断:阻挡请求(一种简单的防火墙)

四:第二个需要注意的问题:注解形式时,过滤器执行的顺序(仅供了解,因为在实际工作中基本不会采用这种策略)


一:过滤链简介

如下图,过滤器1,过滤器2,Servlet这三者组成了一个完整的过滤链。

注意事项:即chain.doFilter()向后传递的时候是根据web.xml中<filter-mapping>的书写顺序为依据的;


二:过滤链案例

案例:包括,FilterA,FilterB,FilterC三个过滤器,web.xml配置文件,HelloServlet这个Servlet:

web.xml:循规蹈矩的配置:

<?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" id="WebApp_ID" version="3.1">
  <display-name>filter-chain</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>filterAA</filter-name>
  	<filter-class>com.imooc.filter.FilterA</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>filterAA</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <filter>
  	<filter-name>filterBB</filter-name>
  	<filter-class>com.imooc.filter.FilterB</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>filterBB</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值