Filter

2019-09-18  本文已影响0人  iDevOps

过滤器
一般用于完成通用的操作。如:登录验证、统一编码处理、敏感字符过滤

# 注解方式配置拦截路径
@WebFilter("/*")
public class FilterDemo implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("FilterDemo ...... ");
        //放行
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
# web.xml中配置拦截路径
<filter>
    <filter-name>demo</filter-name>
    <filter-class>cn.test.web.filter.FilterDemo</filter-class>
</filter>
<filter-mapping>
    <filter-name>demo</filter-name>
    <!-- 拦截路径 -->
    <url-pattern>/*</url-pattern>
</filter-mapping>
@WebFilter("/*")
public class FilterDemo implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //1. 执行过滤器
        System.out.println("执行过滤器.....");
        //2. 执行放行后的资源
        filterChain.doFilter(servletRequest, servletResponse);
        //3. 回头继续执行这里的代码
    }

    @Override
    public void destroy() {

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

    /**
     * 在服务器启动时, 会创建Filter对象, 然后调用init方法, 只执行一次, 用于加载资源
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    /**
     * 每一次请求被拦截时, 都会执行
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    }

    /**
     * 在服务器关闭后,Filter对象被销毁。如果服务器是正常关闭,则会执行destroy方法。只执行一次。用于释放资源
     */
    @Override
    public void destroy() {

    }

}
//拦截路径配置
@WebFilter("/index.jsp")  //只有访问index.jsp资源时,过滤器才会被执行
@WebFilter("/user/*")  //访问/user下的所有资源时,过滤器都会被执行
@WebFilter("*.jsp")   //访问所有后缀名为jsp资源时,过滤器都会被执行
@WebFilter("/*")   //访问所有资源时,过滤器都会被执行

//拦截方式配置
@WebFilter(value = "/*", dispatcherTypes = DispatcherType.REQUEST)  //默认值。浏览器直接请求资源
@WebFilter(value = "/*", dispatcherTypes = DispatcherType.FORWARD)  //转发访问资源
@WebFilter(value = "/*", dispatcherTypes = DispatcherType.INCLUDE)  //包含访问资源
@WebFilter(value = "/*", dispatcherTypes = DispatcherType.ERROR)    //错误跳转资源
@WebFilter(value = "/*", dispatcherTypes = DispatcherType.ASYNC)    //异步访问资源
public class FilterDemo implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    }

    @Override
    public void destroy() {

    }

}

过滤器执行顺序, 加入有两个过滤器

过滤器1
过滤器2
资源执行
过滤器2
过滤器1 

配置
注解配置: 按照类名的字符串比较规则比较,值小的先执行
web.xml配置:<filter-mapping>谁定义在上边,谁先执行

上一篇下一篇

猜你喜欢

热点阅读