过滤器相对比较简单了
代码语言:javascript复制public class WebFiler implements Filter{
//在bean销毁的时候执行
@Override
public void destroy() {
System.out.println("destroy");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filter) throws IOException, ServletException {
System.out.println("前");//拦截处理逻辑
filter.doFilter(request, response);//正常执行
System.out.println("后");//返回拦截
}
//在bean初始化时候执行
@Override
public void init(FilterConfig arg0) throws ServletException {
arg0.getServletContext();
System.out.println("init");
}
}
web.xml配置过滤器
代码语言:javascript复制 <filter>
<filter-name>WebFilter</filter-name>
<!-- 配置过滤器的路径,切记类必须实现Filter接口 -->
<filter-class>com.cn.cis.interceptor.WebFiler</filter-class>
</filter>
<filter-mapping>
<filter-name>WebFilter</filter-name>
<!-- 配置过滤器的路径,配置需要被过滤器拦截的 -->
<url-pattern>/*</url-pattern>
</filter-mapping>