目录
三:第一个需要注意的问题: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