在Servlet规范2.3中定义了过滤器,它能够对Servlet容器的请求和响应对象进行检查和修改。
Servlet过滤器本身并不生成请求和响应对象,只是提供过滤功能。
Servlet过滤器能够在Servlet被调用之前检查Request对象,并修改Request Header和Request内容;
在Servlet被调用之后检查Response对象,修改Response Header和Response的内容。
Servlet过滤器可以过滤的Web组件包括Servlet,JSP和HTML等文件。
Filter类似于IO中的过滤流,实现也类似于Servlet。
所有的Servlet过滤器都必须实现javax.servlet.Filter接口,并实现该接口中的三个方法:
init(FilterConfig filterConfig)
Servlet过滤器的初始化方法,Servlet容器创建Servlet过滤器实例后将调用该方法。该方法将读取web.xml文件中Servlet过滤器的初始化参数。
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
该方法完成实际的过滤操作,当客户端请求方法与过滤器设置匹配的URL时,Servlet容器将先调用过滤器的doFilter方法。FilterChain用户访问后续过滤器。
这里的ServletRequest和ServletResponse一般需要转换成具体的Servlet实现对于的对象,如:HttpServletRequest和HttpServletResponse。
destroy()
Servlet容器在销毁过滤器实例前调用该方法,在该方法中释放Servlet过滤器占用的资源。
public interface Filter
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.
Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.
Filters are configured in the deployment descriptor of a web application Examples that have been identified for this design are 1) Authentication Filters 2) Logging and Auditing Filters 3) Image conversion Filters 4) Data compression Filters 5) Encryption Filters 6) Tokenizing Filters 7) Filters that trigger resource access events 8) XSL/T filters 9) Mime-type chain Filter
public interface FilterChain
A FilterChain is an object provided by the servlet container to the developer giving a view into the invocation chain of a filtered request for a resource. Filters use the FilterChain to invoke the next filter in the chain, or if the calling filter is the last filter in the chain, to invoke the resource at the end of the chain.
Filter的init方法中提供了一个FilterConfig对象,提供相关的操作:
<filter> <filter-name>LoginFilter</filter-name> <filter-class>com.itzhai.login.LoginFilter</filter-class> <init-param> <param-name>username</param-name> <param-value>arthinking</param-value> </init-param> </filter>
在init方法中获取:
@Override public void init(FilterConfig filterConfig) throws ServletException { //获取Filter初始化参数 String username = filterConfig.getInitParameter("username"); }
ServletContext context = filterConfig.getServletContext();
也可以在doFilter方法中根据转换好的request获取:
HttpServletRequest req = (HttpServletRequest)request; ServletContext context = req.getSession().getServletContext();
编写Filter过滤器:
public class LoginFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init LoginFilter"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //把ServletRequest和ServletResponse转换成真正的类型 HttpServletRequest req = (HttpServletRequest)request; HttpSession session = req.getSession(); //由于web.xml中设置Filter过滤全部请求,可以排除不需要过滤的url String requestURI = req.getRequestURI(); if(requestURI.endsWith("login.jsp")){ chain.doFilter(request, response); return; } //判断用户是否登录,进行页面的处理 if(null == session.getAttribute("user")){ //未登录用户,重定向到登录页面 ((HttpServletResponse)response).sendRedirect("login.jsp"); return; } else { //已登录用户,允许访问 chain.doFilter(request, response); } } @Override public void destroy() { System.out.println("destroy!!!"); } }
在web.xml中配置Filter:
<filter> <filter-name>LoginFilter</filter-name> <filter-class>com.itzhai.login.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注意:一般Filter配置在所有的Servlet之前。
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //转换成实例的请求和响应对象 HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse resp = (HttpServletResponse)response; //获取评论并屏蔽关键字 String comment = req.getParameter("comment"); comment = comment.replace("A", "***"); //重新设置参数 req.setAttribute("comment", comment); //继续执行 chain.doFilter(request, response); }
Filter的执行顺序与在web.xml配置文件中的配置顺序一致,一般把Filter配置在所有的Servlet之前。