Filter接口
Filter也称之为过滤器,通过Filter对所有web资源(例如Jsp, Servlet, 静态文件等)进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等。
package javax.servlet;
/**
* A filter is an object that performs filtering tasks on either the
* request to a resource (a servlet or static content), or on the response
* from a resource, or both.
*
* @since Servlet 2.3
*/
public interface Filter {
public void init(FilterConfig filterConfig) throws ServletException;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException;
public void destroy();
} Filter的创建和销毁由WEB服务器负责。 web 应用程序启动时,web 服务器将创建Filter的实例对象,并调用其init方法,完成对象的初始化功能,从而为后续的用户请求作好拦截的准备工作,filter对象只会创建一次,init方法也只会执行一次。通过init方法的参数,可获得代表当前filter配置信息的FilterConfig对象。