我是靠谱客的博主 时尚学姐,最近开发中收集的这篇文章主要介绍Filter的过滤器链Filter的介绍过滤器链,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Filter的介绍

过滤器链

我们已经了解了过滤器,并且知道可以配多个过滤器,那么多个滤器之间的执行顺序是什么样的呢

我们建立8个过滤器,demo1,demo2,demo3,和demo11在web.xml配置,demo4,demo5,demo6,和demo41用注解配置

demo1代码

package com.lingaolu.filter;

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

/**
 * @author 林高禄
 * @create 2020-08-04-9:42
 */
public class Demo1 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("demo【1】过滤器init进来了......");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("demo【1】过滤器进来了......");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("demo【1】过滤回来了......");
    }

    @Override
    public void destroy() {
        System.out.println("demo【1】过滤器destroy进来了......");
    }
}

demo2代码

package com.lingaolu.filter;

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

/**
 * @author 林高禄
 * @create 2020-08-05-15:12
 */
public class Demo2 implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("demo【2】过滤器init进来了......");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("demo【2】过滤器进来了......");
        filterChain.doFilter(request,response);
        System.out.println("demo【2】过滤回来了......");
    }

    @Override
    public void destroy() {
        System.out.println("demo【2】过滤器destroy进来了......");
    }

}

demo3代码

package com.lingaolu.filter;

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

/**
 * @author 林高禄
 * @create 2020-08-05-15:12
 */
public class Demo3 implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("demo【3】过滤器init进来了......");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("demo【3】过滤器进来了......");
        filterChain.doFilter(request,response);
        System.out.println("demo【3】过滤回来了......");
    }

    @Override
    public void destroy() {
        System.out.println("demo【3】过滤器destroy进来了......");
    }

}

demo11代码

package com.lingaolu.filter;

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

/**
 * @author 林高禄
 * @create 2020-08-05-15:12
 */
public class Demo11 implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("demo【11】过滤器init进来了......");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("demo【11】过滤器进来了......");
        filterChain.doFilter(request,response);
        System.out.println("demo【11】过滤回来了......");
    }

    @Override
    public void destroy() {
        System.out.println("demo【11】过滤器destroy进来了......");
    }

}

上面4个过滤器的配置在web.xml里面,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_4_0.xsd"
         version="4.0">

    <filter>
        <filter-name>demo1</filter-name>
        <filter-class>com.lingaolu.filter.Demo1</filter-class>
    </filter>
    <filter>
        <filter-name>demo2</filter-name>
        <filter-class>com.lingaolu.filter.Demo2</filter-class>
    </filter>
    <filter>
        <filter-name>demo3</filter-name>
        <filter-class>com.lingaolu.filter.Demo3</filter-class>
    </filter>
    <filter>
        <filter-name>demo11</filter-name>
        <filter-class>com.lingaolu.filter.Demo11</filter-class>
    </filter>


    <filter-mapping>
        <filter-name>demo2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>demo1</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>demo3</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>demo11</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

这里故意打乱filter-mapping的顺序为demo2,demo1,demo3,demo11,注意这个顺序

下面4个过滤器是用注解配置

demo4代码

package com.lingaolu.filter;

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

/**
 * @author 林高禄
 * @create 2020-08-05-15:12
 */
@WebFilter("/*")
public class Demo4 implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("demo【4】过滤器init进来了......");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("demo【4】过滤器进来了......");
        filterChain.doFilter(request,response);
        System.out.println("demo【4】过滤回来了......");
    }

    @Override
    public void destroy() {
        System.out.println("demo【4】过滤器destroy进来了......");
    }

}

demo5代码

package com.lingaolu.filter;

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

/**
 * @author 林高禄
 * @create 2020-08-05-15:12
 */
@WebFilter("/*")
public class Demo5 implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("demo【5】过滤器init进来了......");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("demo【5】过滤器进来了......");
        filterChain.doFilter(request,response);
        System.out.println("demo【5】过滤回来了......");
    }

    @Override
    public void destroy() {
        System.out.println("demo【5】过滤器destroy进来了......");
    }

}

demo6代码

package com.lingaolu.filter;

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

/**
 * @author 林高禄
 * @create 2020-08-05-15:12
 */
@WebFilter("/*")
public class Demo6 implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("demo【6】过滤器init进来了......");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("demo【6】过滤器进来了......");
        filterChain.doFilter(request,response);
        System.out.println("demo【6】过滤回来了......");
    }

    @Override
    public void destroy() {
        System.out.println("demo【6】过滤器destroy进来了......");
    }

}

demo41代码

package com.lingaolu.filter;

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

/**
 * @author 林高禄
 * @create 2020-08-05-15:12
 */
@WebFilter("/*")
public class Demo41 implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("demo【41】过滤器init进来了......");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("demo【41】过滤器进来了......");
        filterChain.doFilter(request,response);
        System.out.println("demo【41】过滤回来了......");
    }

    @Override
    public void destroy() {
        System.out.println("demo【41】过滤器destroy进来了......");
    }

}

我们启动项目

可见初始化没有什么规律

访问index.jsp

再访问一次,也还是这个输出,2->1->3->11->4->41->5->6

  • 前面4个2->1->3->11是在web.xml配置的,并且顺序是按filter-mapping的顺序来的,和filter的顺序无关
  • 后面4个4->41->5->6是用注解配置的,从顺序来看是名字的字母排序顺序来的
  • 所以过滤器的执行顺序是,先执行web.xml配置的,并且顺序是按filter-mapping的顺序来的,再执行用注解配置的,并且顺序是按名字的字母排序顺序来的

我们正常关闭项目

从结果上来看,没有什么规律,但是和初始化的顺序是一样的,也就时destroy方法的执行顺序由过滤器的初始化就决定了顺序。

最后

以上就是时尚学姐为你收集整理的Filter的过滤器链Filter的介绍过滤器链的全部内容,希望文章能够帮你解决Filter的过滤器链Filter的介绍过滤器链所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(29)

评论列表共有 0 条评论

立即
投稿
返回
顶部