1.介绍
2.实现
/**
* 拦截器
* 如果是过滤器链,则先配置的先执行(首字符在前的先执行);响应时顺序是反的;
*/
@WebFilter("/*")//拦截所有
public class Filter01 implements Filter {
/**
* 初始化方法
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter init ....");
}
/**
* 拦截方法
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Filter doFilter ....");
//doFilter()放行方法前去做请求拦截的
//放行资源 把servletRequest和servletResponse外放
filterChain.doFilter(servletRequest,servletResponse);
//doFilter()放行方法后去做响应拦截的
}
/**
* 销毁方法
*/
@Override
public void destroy() {
System.out.println("Filter destroy ....");
}
}