过滤链的一点问题
package filter; 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 FilterC implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } chain.doFilter(request, response); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("I am " + this.getClass().getSimpleName()); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
package filter; 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 FilterB implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub chain.doFilter(request, response); System.out.println("I am " + this.getClass().getSimpleName()); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
package filter; 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 FilterA implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub chain.doFilter(request, response); System.out.println("I am " + this.getClass().getSimpleName()); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
<?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>A</filter-name> <filter-class>filter.FilterA</filter-class> </filter> <filter> <filter-name>B</filter-name> <filter-class>filter.FilterB</filter-class> </filter> <filter> <filter-name>C</filter-name> <filter-class>filter.FilterC</filter-class> </filter> <filter-mapping> <filter-name>A</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>B</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>C</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
package filter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloServlet */ @WebServlet("/hello") public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().println("hello world"); System.out.println("hello world"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
只有第一个class里面我在chain.dofilter方法前后各加了休眠,其他的都和老师在2-8里面的代码一样,
问下我这段代码访问servlet以后再控制台会打印出
hello world
I am FilterC
I am FilterB
I am FilterA
不是太能理解,当代码回传到filterC的时候,通过chain.dofilter不是已经像前传递了,同时我在它下面设置了休眠,为什么系统不先打印filterb 和 filtera呢?
我以为会是:
hello world,
I am FilterB
I am FilterA
I am FilterC
这个顺序的。
正在回答
同学你好,chain.doFilter(request, response);请求转发给过滤器链上下一个对象。这里的下一个指的是下一个filter,如果没有filter就执行请求的资源。
所以执行FilterC中的chain.doFilter(request, response);后没有下一个过滤器,则执行请求/hello,执行完HelloServlet,按过滤器链的逆向顺序执行代码,输出FilterC,FilterB,FilterA。
执行chain.doFilter(request, response);并不是将请求传回前面的过滤器,而是传递给后边的过滤器,如果后边没有过滤器,则执行请求。执行请求后,按过滤器逆向顺序执行过滤器链。
祝:学习愉快~
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 9400 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星