Filter 过滤器的细节学习

一、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_3_1.xsd"
         version="3.1">

    <filter>
        <filter-name>demo1</filter-name>
        <!--过滤器文件的路径-->
        <filter-class>FilterDemo</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>demo1</filter-name>
        <!--过滤器的范围-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

二、过滤器执行流程

1)执行过滤器
(2)执行放行后的资源
(3)回来执行过滤器放行代码下边的代码
@WebFilter("/*")
public class FilterTest implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //对request对象请求消息增强
        System.out.println("FilterTest 执行了。。。。");
        //放行
        chain.doFilter(req, resp);
        //对response对象的响应消息增强
        System.out.println("FilterTest 回来了。。。。");
    }

    public void init(FilterConfig config) throws ServletException {

    }

    public void destroy() {

    }
}

三、过滤器生命周期方法

init:在服务器启动后,会创建Filter对象,然后调用init方法,只执行一次,用于加载资源

doFilter:每一次请求被拦截资源时,会执行,执行多次

destroy:在服务器关闭后,Filter对象被销毁。如果服务器是正常关闭,则会执行destroy方法。只执行一次。用于释放资源

在这里插入图片描述
在这里插入图片描述

四、过滤器配置详解

* 拦截路径配置:
	(1)具体资源路径: /index.jsp   只有访问index.jsp资源时,过滤器才会被执行
	(2)拦截目录: /user/*	访问/user下的所有资源时,过滤器都会被执行
	(3)后缀名拦截: *.jsp	访问所有后缀名为jsp资源时,过滤器都会被执行
	(4)拦截所有资源:/*		访问所有资源时,过滤器都会被执行
* 拦截方式配置:资源被访问的方式
	(1)注解配置:
			* 设置dispatcherTypes属性
				* REQUEST:默认值。浏览器直接请求资源
				* FORWARD:转发访问资源
				* INCLUDE:包含访问资源
				* ERROR:错误跳转资源
				* ASYNC:异步访问资源
	(2)web.xml配置
				* 设置<dispatcher></dispatcher>标签即可

直接配置

package Filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(value = "/*",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})
public class FilterTest implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("doFilter 执行了。。。。");
        //放行
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }
}

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_3_1.xsd"
         version="3.1">

    <filter>
        <filter-name>demo1</filter-name>
        <!--过滤器文件的路径,注意路径写法:Filter下含有 FilterDemo -->
        <filter-class>Filter.FilterTest</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>demo1</filter-name>
        <!--过滤器的范围-->
        <url-pattern>/*</url-pattern>
        <!--设置资源被拦截的方式-->
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
</web-app>

五、过滤器链

* 执行顺序:如果有两个过滤器:过滤器1和过滤器2
			* 过滤器1
			* 过滤器2
			* 资源执行
			* 过滤器2
			* 过滤器1 

* 过滤器先后顺序问题:
			* 注解配置:按照类名的字符串比较规则比较,值小的先执行
			* 如: FilterDemoA和 FilterDemoB,FilterDemoA就先执行了。
			* web.xml配置: <filter-mapping>谁定义在上边,谁先执行

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南淮北安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值