filter过滤器的应用
在web.xml中配置过滤器
<filter>
<filter-name>loginfilter</filter-name>
<filter-class>com.zhongruan.filter.LoginFilter</filter-class>
</filter>
<!-- 过滤器的映射 -->
<filter-mapping>
<filter-name>loginfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
实现过滤器类
package com.zhongruan.filter;
import javax.servlet.*;
import java.io.IOException;
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤器执行====");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
System.out.println("过滤器销毁");
}
}