JavaWeb中的filter拦截方式的配置

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

  1. 注解配置:
    设置dispatcherTypes属性
    1. REQUEST:默认值。浏览器直接请求资源
    2. FORWARD:转发访问资源
    3. INCLUDE:包含访问资源
    4. ERROR:错误跳转资源
    5. ASYNC:异步访问资源
  2. web.xml配置
    设置标签即可

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

//拦截方式配置
@WebFilter(value = "/index.jsp", dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
public class FilterDemo5 implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("拦截请求或者转发到index.jsp的资源");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}


posted @ 2021-05-11 21:45  code-G  阅读(472)  评论(0编辑  收藏  举报