责任链的设计模式可以参考Servlet的FilterChain。FilterChain中的每个Filter(过滤器)就像一个个的链条
web开发中 有时候需要对接口request和response进行过滤操作(特殊字符过滤,权限过滤等)
- 接口Filter
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
The doFilter
method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
过滤器Filter中doFliter方法完成实际的过滤操作,通过参数FilterChain把多个过滤器串联起来。
doFilter(ServletRequest request, ServletResponse response)
Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked.
执行fliterChain的doFilter方法,会执行下一个filter的doFilter方法同时FliterChain会当做参数传给下一个Filter。(Filter的执行循序 和在web.xml配置的顺序一致)
责任链设计模式的特点是
过滤器Filter1 code11 chain.doFilter() code12 |
过滤器Filter2 code21 chain.doFilter() code22 |
Servlet的request请求 | Servlet的resopnse响应 |
执行code11后通过chain.doFilter()方法, 代码会到Filter2中去 然后执行code21(如果有下一个filter 会接着执行下一个filter) |
执行完所有的过滤器之后 开始response返回 先从最末的一个过滤器开始返回 于是response时候的代码执行顺序是code22 code12 |
上面的执行顺序会是 code11 code21 code22 code12 FilterChain通过参数传递起到串联所有Filter的作用,通过执行chain.doFilter()来进入下一个Filter
chain.doFilter()以上的代码是对resquest进行过滤,以下的代码对返回response过滤
代码证明:
1.首先声明一个Servlet
package javaee.net.cn.websocket; 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 MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {; System.out.println("doGet>>>>>>>>>>>>>>>>>>"); } }
2.声明两个过滤器
package javaee.net.cn.websocket; 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 NoteFilter implements Filter { @Override public void destroy() { System.out.println("note destory<<<<<<<<<<<<<<<<"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("request note filter"); chain.doFilter(request, response); System.out.println("response note filter"); } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("note init<<<<<<<<<<<<<<"); } }
package javaee.net.cn.websocket; 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 HtmlFilter implements Filter { @Override public void destroy() { System.out.println("html destory<<<<<<<<<<<<<<<<"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("request html filter"); chain.doFilter(request, response); System.out.println("response html filter"); } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("html init<<<<<<<<<<<<<<"); } }
3.在web.xml中配置Servlet和过滤器(在web.xml中 必须先配置所有的过滤器 在配置Servlet)
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Tab3</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list> <filter> <filter-name>NoteFilter</filter-name> <filter-class>javaee.net.cn.websocket.NoteFilter</filter-class> </filter> <filter-mapping> <filter-name>NoteFilter</filter-name> <url-pattern>/myServlet</url-pattern> </filter-mapping> <filter> <filter-name>HtmlFilter</filter-name> <filter-class>javaee.net.cn.websocket.HtmlFilter</filter-class> </filter> <filter-mapping> <filter-name>HtmlFilter</filter-name> <url-pattern>/myServlet</url-pattern> </filter-mapping>
<!--上面配置fliter 下面配置Servlet --> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>javaee.net.cn.websocket.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myServlet</url-pattern> </servlet-mapping> </web-app>
4.浏览器输入web.xml配置的url路径:http://127.0.0.1:8080/myServlet
输出结果是:
request note filter
request html filter
doGet>>>>>>>>>>>>>>>>>>
response html filter
response note filter
和上面分析一致