首页 > 其他分享 >Filter_细节_过滤器链(多个过滤器)与Filter_案例1_登录验证_分析

Filter_细节_过滤器链(多个过滤器)与Filter_案例1_登录验证_分析

时间:2023-02-20 16:13:53浏览次数:30  
标签:登录 void Filter 过滤器 import public

Filter_细节_过滤器链(多个过滤器)

         1.web.xml配置

         2.过滤器的执行流程

         3.过滤器的生命周期方法

         4.过滤器配置详解

         5.过滤器链(配置 多个过滤器)

             执行顺序:如果两个过滤器:过滤器1和过滤器2

              1.过滤器1

              2.过滤器2

              3.过滤执行

              4.过滤器2

              5.过滤器1

                

      

package hf.xueqiang.filter;

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

@WebFilter("/*")
public class FilterDemo6 implements Filter {


    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        System.out.println("filterDemo6执行了.....");
        chain.doFilter(request, response);
        System.out.println("FilterDemo6下班了.....");
    }

    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }


}

 

package hf.xueqiang.filter;

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

@WebFilter("/*")
public class FilterDemo7 implements Filter {


    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        System.out.println("filterDemo7执行了.....");
        chain.doFilter(request, response);
        System.out.println("FilterDemo7下班了.....");
    }

    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }


}

 

               过滤器先后顺序问题:

                  1.注解配置:按照类名的字符串比较规则比较,值小的先执行 

                     如:AFilter和BFilter, AFilter就先执行了。 

                  

 

 

                  2.web.xml配置: <filter-mapping> 谁定义在上边, 谁先执行

Filter_案例1_登录验证_分析

        案例:

         案例1_登录验证

        需求:

         1. 访问day17_case案例的资源。验证其是否登录
         2. 如果登录了,则直接放行。
         3. 如果没有登录,则跳转到登录页面,提示"您尚未登录,请先登录"。

          1.判断是否是登录相米的资源
              是:直接放行

              不是:判断是否登录

          2.判断当前用户是否登录,判断Session中是否有User

              有:已经登录,放行
              没有:没有登录。跳转到登录页面

 

 

    

    

标签:登录,void,Filter,过滤器,import,public
From: https://www.cnblogs.com/x3449/p/17137758.html

相关文章