赞
踩
- 一个同学进校园,先会遇到保安,如果该同学出校园,也会碰到保安
- 过滤器的执行流程
- tomcat引擎将http请求转成request对象,http响应转response对象
- 地址符合条件,则将request,resposne传给doFilter方法
- 再传给chain.doFilter(request,response)方法
- 再传给doGet或者doPost方法,当servlet完成三件事之后
- doFilter再执行剩余代码之后,reponse再返回浏览器
- 设置编码的代码位置
- doFilter之前设置请求编码,之前设置响应编码
##filter-过滤器的执行顺序
- 如果有两个Filter都过滤请求,那么执行顺序是如何的?
先Filter1,再Filter2,最后Servlet,响应返回时相反
什么设置决定Filter器的执行顺序?
解配置
当有多个过滤器时,是按照过滤器类的首字母的先后顺序执行
先AFilter再BFilter
web.xml配置
当有多个过滤器时,是按照web.xml中 <filter-mapping></filter-mapping>
配置的先后先后顺序执行的
##代码案例
AFilter:
package com.lbl.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter("/s2") public class AFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("第1个过滤器收到请求"); chain.doFilter(req, resp); System.out.println("第1个过滤器收到响应"); } public void init(FilterConfig config) throws ServletException { } }
BFilter
package com.lbl.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter("/s2") public class BFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("第2个过滤器收到请求"); chain.doFilter(req, resp); System.out.println("第2个过滤器收到响应"); } public void init(FilterConfig config) throws ServletException { } }
Demo02Servlet
package com.lbl.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/s2") public class Demo02Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet执行了"); } }
运行效果:
OneFilter
package com.lbl.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; //@WebFilter("/s3") public class OneFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("第one个过滤器收到请求"); chain.doFilter(req, resp); System.out.println("第one个过滤器收到响应"); } public void init(FilterConfig config) throws ServletException { } }
TwoFilter
package com.lbl.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; //@WebFilter("/s3") public class TwoFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("第two个过滤器收到请求"); chain.doFilter(req, resp); System.out.println("第two个过滤器收到响应"); } public void init(FilterConfig config) throws ServletException { } }
Demo03Servlet
package com.lbl.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; //@WebServlet("/s3") public class Demo03Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Demo03Servlet....servlet执行了"); } }
Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>Demo3Servlet</servlet-name> <servlet-class>com.lbl.servlet.Demo03Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Demo3Servlet</servlet-name> <url-pattern>/s3</url-pattern> </servlet-mapping> <filter> <filter-name>TwoFilter</filter-name> <filter-class>com.lbl.filter.TwoFilter</filter-class> </filter> <filter-mapping> <filter-name>TwoFilter</filter-name> <url-pattern>/s3</url-pattern> </filter-mapping> <filter> <filter-name>OneFilter</filter-name> <filter-class>com.lbl.filter.OneFilter</filter-class> </filter> <filter-mapping> <filter-name>OneFilter</filter-name> <url-pattern>/s3</url-pattern> </filter-mapping> </web-app>
运行效果:
过滤器有3个方法:
- init方法:服务器启动时就创建该filter对象
- doFilter方法:每当一个请求的路径是满足过滤器的配置路径,那么就会执行一次过滤器的doFilter方法
- destory方法:服务器关闭时filter销毁
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。