Java Servlet 过滤器与 SpringMVC 拦截器的区别?(5)

/**
 * 过滤器
 * @author yule
 * @date 2018/7/2 21:52
 */
public class DemoFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("demo过滤器init。。。");
    }

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("demo过滤器doFilter。。。此处省略业务处理逻辑");

//通过判断是否继续往下走
        filterChain.doFilter(servletRequest, servletResponse);
    }

@Override
    public void destroy() {
        System.out.println("demo过滤器destroy。。。");
    }
}

package com.yule.common.filters;

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

/**
 * Created by yule on 2018/7/2 22:18.
 */
public class Demo2Filter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("demo2过滤器init  2222222");
    }

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("demo过滤器doFilter  222222");

//通过判断是否继续往下走
        filterChain.doFilter(servletRequest, servletResponse);
    }

@Override
    public void destroy() {
        System.out.println("demo2过滤器destroy  22222 ");
    }
}

调用 controller 打印结果:

Java Servlet 过滤器与 SpringMVC 拦截器的区别?

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/893166aeea1e25d7761addd2482f5202.html