Filter_细节_过滤器拦截路径配置与Filter_细节_过滤器拦截方式配置

Filter_细节_过滤器拦截路径配置

    

 过滤细节:

         1.web.xml配置

         2.过滤器的执行流程

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

         4.过滤器配置详解

            拦截路径配置:

              1.具体资源路径:/index.jsp  只有访问indexjsp资源时候,过滤器才会被执行

.        

执行:index.jsp

执行:hello.jsp

              2.拦截目录: /user/*  访问/user下的所有资源部时,过滤器才会被执行  

 

 

 

              3.后缀名:*.jsp      访问所有后缀名jsp资源时,过滤器都会被执行

 

 

 

 

 

 

              4.拦截所有资源:/*     访问所有资源时候,过滤器都会被执行 

 

 

 

 

 

            拦截方式配置:

            

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

  

Filter_细节_过滤器拦截方式配置

    

过滤细节:

         1.web.xml配置

         2.过滤器的执行流程

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

         4.过滤器配置详解
            拦截路径配置


            拦截方式配置:资源被访问的方式  

               注解配置:

                 设置dispatcherTypes属性

                    1.REQUEST:默认值、浏览器直接请求资源

          

 

 

 

          

 

 

 

  

                    2.FORWARD:转发访问资源

      

 

 

 

    

      

 

 

       

 

      

 

 

 

                    3.INCLUDE :包含访问资源

                    4.ERROR:错误跳转资源

                    5.ASYNC:异步访问资源 

               web.xml配置  

                 设置<dispatcher></dispatcher>标签即可

          

package hf.xueqiang.filter;

import javax.servlet.*;
import java.io.IOException;
//浏览器直接请求index.jsp资源时,该过滤器会被执行
//@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.REQUEST)
//只有转发访问index.jsp时,该过滤器才会被执行
//@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.FORWARD)
//浏览器直接请求index.jsp或者只有转发访问index.jsp。该过滤器才会被执行
//@WebFilter(value ="/index.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})
public class FilterDemo5 implements Filter {


    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        System.out.println("FilterDemo5****************");
        chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }


}

 

package hf.xueqiang.servlet;

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

@WebServlet("/user/findAllServlet")
public class ServletDemo1 extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("findAllServlet.........");
    }
     @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }
}

 

 

package hf.xueqiang.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("/user/updateServlet")
public class ServletDemo2 extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("updateServlet.........");


        //转发到index.jsp
        request.getRequestDispatcher("/index.jsp").forward(request,response);
    }
     @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }
}

 


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

posted @ 2023-02-20 14:34  zj勇敢飞,xx永相随  阅读(56)  评论(0编辑  收藏  举报