filterchain
过滤器拦截到请求之后,首先是执行doFilter()方法中chain.doFilter()之前的代码,然后放弃权限给下一个过滤器或者serverlet等等,最后才执行chain.doFilter()之后的代码。
测试场景:
1.新建一个index.html,配置连接调整到一个test.jsp页面
2.对test.jsp做两层过滤,查看过滤器代码中chain.doFilter()之前和之后代码的执行顺序。
过滤器一:(使用注解控制过滤器执行顺序,此处先执行过滤器一,在执行过滤器2)
@WebFilter(filterName="firstFilter0",urlPatterns="/test.jsp")
public class FirstFilter implements Filter {
@Override
public void destroy() {
System.out.println("first destroy...");
}
@Override
public void doFilter(Servletrequest arg0, Servletresponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("1. first doFilter...");
arg2.doFilter(arg0, arg1);
System.out.println("2. first doFilter...");
}
@Override
public void init(filterconfig arg0) throws ServletException {
System.out.println("first init...");
}
}
过滤器二:
@WebFilter(filterName="firstFilter1",urlPatterns="/test.jsp")
public class SecondFilter implements Filter {
@Override
public void destroy() {
System.out.println("second destroy...");
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("3. second doFilter...");
arg2.doFilter(arg0, arg1);
System.out.println("4. second doFilter...");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("second init...");
}
}
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="test.jsp">To Test Page</a>
</body>
</html>
test.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>Test Page</h4>
<%
System.out.println("5. Test Page");
%>
</body>
</html>
在浏览器输入http://localhost:8080/day_38/index.html控制台将打印如下内容 :
1. first doFilter...
3. second doFilter...
5. Test Page
4. second doFilter...
2. first doFilter...
相关阅读
【Matlab】conv2、filter2、imfilter的区别
http://www.ilovematlab.cn/thread-293710-1-1.html -------------------------------------conv2函数-------------------------
源代码加图片加背景音乐链接:https://pan.baidu.com/s/1Ll_vVsBC-lYNpLrSRAz5ew提取码:h2ds复制这段内容后打开百度网盘手机App,操作
参考:CRC校验原理及步骤https://blog.csdn.net/d_leo/article/details/73572373 什么是CRC校验? CRC即循环冗余校验码:是数据通信领
今日团购: 限时活动,机会难得!如此优惠,机不可失,把握好机会哦!
一、逗号, 之后加空格 printf(" error! score[%d] = %d\n", i, score[i]); 二、分号; 之后加空格 for(i = 0; i < student_n