当前位置:   article > 正文

javaWeb过滤器--filter-过滤器的执行流程_web开发里的过滤器的注解的顺序

web开发里的过滤器的注解的顺序

javaWeb过滤器–filter-过滤器的执行流程

filter-过滤器的执行流程

  • 一个同学进校园,先会遇到保安,如果该同学出校园,也会碰到保安
  • 过滤器的执行流程
    • tomcat引擎将http请求转成request对象,http响应转response对象
    • 地址符合条件,则将request,resposne传给doFilter方法
    • 再传给chain.doFilter(request,response)方法
    • 再传给doGet或者doPost方法,当servlet完成三件事之后
    • doFilter再执行剩余代码之后,reponse再返回浏览器
  • 设置编码的代码位置
    • doFilter之前设置请求编码,之前设置响应编码
      在这里插入图片描述

##filter-过滤器的执行顺序

img

  • 如果有两个Filter都过滤请求,那么执行顺序是如何的?
    先Filter1,再Filter2,最后Servlet,响应返回时相反
  • 什么设置决定Filter器的执行顺序?

  • 解配置
    当有多个过滤器时,是按照过滤器类的首字母的先后顺序执行

    先AFilter再BFilter

  • web.xml配置
    当有多个过滤器时,是按照web.xml中 <filter-mapping></filter-mapping>配置的先后先后顺序执行的

##代码案例

AFilter:

package com.lbl.filter;

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

@WebFilter("/s2")
public class AFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("第1个过滤器收到请求");
        chain.doFilter(req, resp);
        System.out.println("第1个过滤器收到响应");
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

BFilter

package com.lbl.filter;

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

@WebFilter("/s2")
public class BFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("第2个过滤器收到请求");
        chain.doFilter(req, resp);
        System.out.println("第2个过滤器收到响应");
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

Demo02Servlet

package com.lbl.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/s2")
public class Demo02Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("servlet执行了");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

运行效果:
在这里插入图片描述

注释@WebFilter,改成xml配置

OneFilter

package com.lbl.filter;

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

//@WebFilter("/s3")
public class OneFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("第one个过滤器收到请求");
        chain.doFilter(req, resp);
        System.out.println("第one个过滤器收到响应");
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

TwoFilter

package com.lbl.filter;

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

//@WebFilter("/s3")
public class TwoFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("第two个过滤器收到请求");
        chain.doFilter(req, resp);
        System.out.println("第two个过滤器收到响应");
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

Demo03Servlet

package com.lbl.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebServlet("/s3")
public class Demo03Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Demo03Servlet....servlet执行了");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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

    <servlet>
        <servlet-name>Demo3Servlet</servlet-name>
        <servlet-class>com.lbl.servlet.Demo03Servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Demo3Servlet</servlet-name>
        <url-pattern>/s3</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>TwoFilter</filter-name>
        <filter-class>com.lbl.filter.TwoFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>TwoFilter</filter-name>
        <url-pattern>/s3</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>OneFilter</filter-name>
        <filter-class>com.lbl.filter.OneFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OneFilter</filter-name>
        <url-pattern>/s3</url-pattern>
    </filter-mapping>
</web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

运行效果:

在这里插入图片描述

filter-过滤器的生命周期(了解)

过滤器有3个方法:

  • init方法:服务器启动时就创建该filter对象
  • doFilter方法:每当一个请求的路径是满足过滤器的配置路径,那么就会执行一次过滤器的doFilter方法
  • destory方法:服务器关闭时filter销毁
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/646579
推荐阅读