过滤器拦截路径配置和过滤器拦截方式配置和Filter过滤器链(多个过滤器)

过滤器拦截路径配置

  • 具体资源路径:/index.jsp 只有访问index.jsp资源的时候,过滤器才会被执行
  • 拦截目录:/user/* 访问/user下的所有资源,过滤器都会被执行
  • 后缀名拦截:*.jsp 访问所有后缀名为jsp资源的时候,过滤器都会被执行
  • 拦截所有资源:/* 访问所有资源的时候,过滤器都会被执行
package com.ailyt.filter;


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

//@WebFilter("/index.jsp")//具体资源路径:/index.jsp 只有访问index.jsp资源的时候,过滤器才会被执行
//@WebFilter("/user/*")//拦截目录:/user/* 访问/user下的所有资源,过滤器都会被执行
//@WebFilter("*.jsp")//后缀名拦截:*.jsp 访问所有后缀名为jsp资源的时候,过滤器都会被执行
//@WebFilter("/*")//拦截所有资源:/* 访问所有资源的时候,过滤器都会被执行

public class FilterStudy03 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("FilterStudy03");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }
}

过滤器拦截方式配置

  • 拦截方式配置:资源被访问的方式
    • 注解配置:
      • 设置dispatcherTypes属性
        • REQUEST:默认值。浏览器直接请求资源
        • FORWARD:转发访问资源
        • INCLUDE:包含访问资源
        • ERROR:错误跳转资源
        • ASYNC:异步访问资源
package com.ailyt.filter;


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

////REQUEST:默认值 浏览器直接请求index.jsp资源的时候 该过滤器会被执行
//@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.REQUEST)
//FORWARD:只有转发访问index.jsp的时候,该过滤器才会被执行
//@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.FORWARD)
//浏览器直接请求index.jsp或者转发访问index.jsp的时候过滤器就会被执行
//@WebFilter(value = "/index.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})
//@WebFilter(value = "/*",dispatcherTypes = DispatcherType.INCLUDE)//包含访问资源
//@WebFilter(value = "/*",dispatcherTypes = DispatcherType.ERROR)//错误跳转资源
//@WebFilter(value = "/*",dispatcherTypes = DispatcherType.ASYNC)//异步访问资源
public class FilterStudy04 implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("FilterStudy04");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}

- web.xml配置
	- 设置dispatcher标签即可

image

<?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">

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.ailyt.filter.CharacterEncodingFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/show</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
</web-app>

Filter过滤器链(多个过滤器)

  • 执行顺序:如果有两个过滤器:过滤器1和过滤器2
    • 过滤器1
    • 过滤器2
    • 资源执行
    • 过滤器2
    • 过滤器1
  • 过滤器先后顺序问题:
    • 注解配置:按照类型的字符串比较规则比较,值小的先执行
      • 如AFilter和BFilter,AFilter就先执行了
      • web.xml配置:谁定义在上边谁先执行
package com.ailyt.filter;

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

@WebFilter(filterName = "FilterStudy05",urlPatterns = "/*")
public class FilterStudy05 implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        System.out.println("filterStudy05被执行了……");
        chain.doFilter(request, response);
        System.out.println("filterStudy05回来了……");
    }
}

package com.ailyt.filter;

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

@WebFilter(filterName = "FilterStudy06",urlPatterns = "/*")
public class FilterStudy06 implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        System.out.println("filterStudy06执行了……");
        chain.doFilter(request, response);
        System.out.println("filterStudy06回来了……");
    }
}

image

posted @ 2022-08-18 10:43  我滴妈老弟  阅读(443)  评论(0编辑  收藏  举报