Web.xml过滤器配置及执行顺序概念

来源:互联网 发布:网络教育怎么报名啊 编辑:程序博客网 时间:2024/06/05 01:10
第一个过滤器@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {Boolean b1 = false;if (b1 = true) {// 继续此次请求,如果有多个过滤器,进入下一个过滤器chain.doFilter(request, response);} else {// 重定向此请求((HttpServletResponse) response).sendRedirect("/xxxx");}System.out.println("UrlFilter_1         doFilter...");}-----------------------------------------------------------------------------------第二个过滤器 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {Boolean b2 = false;if (b2 = true) {// 继续此次请求,如果有多个过滤器,进入下一个过滤器chain.doFilter(request, response);} else {// 重定向此请求((HttpServletResponse) response).sendRedirect("/xxxx");}System.out.println("UrlFilter_1         doFilter...");} -----------------------------------------------------------------------------------第三个过滤器 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {Boolean b3 = false;if (b3 = true) {// 继续此次请求,如果有多个过滤器,进入下一个过滤器chain.doFilter(request, response);} else {// 重定向此请求((HttpServletResponse) response).sendRedirect("/xxxx");}System.out.println("UrlFilter_1         doFilter...");}  --------------------------------------------------------------------------------------------Web,xml配置如下<filter><filter-name>url_1</filter-name><filter-class>com.boya.filters.UrlFilter_1</filter-class></filter><filter><filter-name>url_3</filter-name><filter-class>com.boya.filters.UrlFilter_3</filter-class></filter><filter><filter-name>url_2</filter-name><filter-class>com.boya.filters.UrlFilter_2</filter-class></filter><filter-mapping><filter-name>url_3</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter-mapping><filter-name>url_1</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter-mapping><filter-name>url_2</filter-name><url-pattern>/*</url-pattern></filter-mapping>--------------------------------------------------------------------------------------执行结果为 UrlFilter_1         初始化...UrlFilter_3        初始化...UrlFilter_2        初始化...2013-9-29 13:38:40 org.apache.coyote.AbstractProtocol start信息: Starting ProtocolHandler ["http-bio-8080"]2013-9-29 13:38:40 org.apache.coyote.AbstractProtocol start信息: Starting ProtocolHandler ["ajp-bio-8009"]2013-9-29 13:38:40 org.apache.catalina.startup.Catalina start信息: Server startup in 596 msUrlFilter_2         doFilter...UrlFilter_1         doFilter...UrlFilter_3         doFilter...-------------------------------------------------------------------------------------分析结果:1.  chain.doFilter(request, response);  为继续此次请求,如果有下一个过滤器就进去下一个过滤器,没有就直接跳转到请求的链接里。2.  过滤器的执行顺序与xml里面的 <filter-mapping><filter-name>xxx</filter-name><url-pattern>/*</url-pattern></filter-mapping> 有关系,顺序为xml 配置的顺序由下至上 执行。


0 1