一、概述
二、快速入门
/*
过滤器
*/
@WebFilter("/*") // 访问所有资源之前,都会执行该过滤器
public class FilterDemo1 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filterDemo1执行了");
// 放行
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
测试结果:
过滤器输出:
三、过滤器细节设置
1、web.xml配置
首先要找到web-INF目录下的web.xml文件,新建module的时候勾选上
<?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_3_1.xsd"
version="3.1">
<filter>
<filter-name>demo1</filter-name>
<filter-class>cn.itcast.web.filter.FilterDemo1</filter-class>
</filter>
<filter-mapping>
<filter-name>demo1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
可以看到同样可以通过web-xml设置:
2、过滤器执行流程
直接新建Filter
在index.jsp中修改如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
Filter被执行
<%
System.out.println("index.jsp...");
%>
</body>
</html>
新建filter
@WebFilter("/*")
public class FilterDemo2 implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
// 对request对象的增强
System.out.println("filterdemo2...");
// 放行
chain.doFilter(req, resp);
// 对response消息的增强
System.out.println("filterdemo2 come");
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}
测试如下:
执行流程:
先经过过滤器,然后执行放行后的资源,再回来执行过滤器后面的代码
3、过滤器生命周期方法
init方法:在服务器启动后,会创建Filter对象,然后就会调用init方法,只会执行一次,用于加载资源;
destroy方法:在服务器关闭后,Filter对象被销毁,如果服务器是正常关闭,执行destroy方法,只会执行一次,用于释放资源;
doFilter方法:每一次请求被拦截资源时,会执行,执行多次;
注意:访问demo3的时候把前面例子的WebFilter路径注释掉。
@WebFilter("/*")
public class FilterDemo3 implements Filter {
/*
每一次请求被拦截资源时,会执行,执行多次
*/
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("doFilter..");
chain.doFilter(req, resp);
}
/*
在服务器启动后,会创建Filter对象,然后就会调用init方法,只会执行一次
*/
public void init(FilterConfig config) throws ServletException {
System.out.println("init..");
}
/*
在服务器关闭后,Filter对象被销毁,如果服务器是正常关闭,执行destroy方法,只会执行一次
*/
public void destroy() {
System.out.println("destroy..");
}
}
测试如下:
4、过滤器配置详解
拦截路径配置:@WebFilter()
具体资源路径:/index.jsp,只有访问这个资源才会执行过滤器
目录拦截:/user/* :user下的所有资源时,过滤器执行
后缀:*.jsp 访问所有的jsp文件时,过滤器执行
拦截所有资源:/* 所有路径,过滤器执行
拦截方式配置:资源被访问的方式
注解配置
设置dispatcherTypes属性
REQUEST:默认值,浏览器直接请求资源
FORWORD:转发访问资源
INCLUDE:包含访问资源
ERROR:错误跳转资源
ASYNC:异步
默认模式:
// 浏览器直接请求index.jsp资源时,过滤器被执行
@WebFilter(value="/index.jsp",dispatcherTypes = DispatcherType.REQUEST)
public class FilterDemo4 implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("filterDemo4");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
过滤器被执行:
编辑转发模式:
@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("updateServlet");
// 转发到index.jsp
request.getRequestDispatcher("index.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
转发属性过滤器没被执行:
希望转发的时候执行:
@WebFilter(value="/index.jsp",dispatcherTypes = DispatcherType.FORWARD)
public class FilterDemo4 implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("filterDemo4");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}
测试:
直接访问不到
转发模式过滤器生效:
如果想要既可以转发访问,又可以直接访问
修改路径:
@WebFilter(value="/index.jsp",dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
web.xml配置
设置<dispatcher></dispatcher>,属性依然是上面五个
5、过滤器链
多个过滤器可以同时生效
执行顺序:
如果有两个过滤器Filter1,Filter2。
Filter1先执行,然后执行Filter2,然后资源执行。然后执行Filter2,最后执行Filter1
@WebFilter("/*")
public class FilterDemo5 implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("demo5执行");
chain.doFilter(req, resp);
System.out.println("demo5回来了");
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}
@WebFilter("/*")
public class FilterDemo6 implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("demo6执行");
chain.doFilter(req, resp);
System.out.println("demo6回来了");
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}
测试结果如下:
关于过滤器执行先后顺序问题:
注解配置,按照类名的字符串比较规则比较,值小的先执行
比如:Afilter 和Bfilter,A一定先执行
如果想让6先执行,在6的后面加1,写成FilterDemo61
关于配置web.xml执行顺序:
<filter-mapping>谁定义在上面,谁先执行。
get!