JDK设计模式之——责任链
Posted ssskkk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK设计模式之——责任链相关的知识,希望对你有一定的参考价值。
责任链的设计模式可以参考Servlet的FilterChain。FilterChain中的每个Filter(过滤器)就像一个个的链条
- 接口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把多个过滤器串联起来。
- 参数FliterChain
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
代码证明:
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和过滤器
<?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
和上面分析一致
以上是关于JDK设计模式之——责任链的主要内容,如果未能解决你的问题,请参考以下文章