过滤器 API 包含 3 个简单的接口,它们整洁地嵌套在 javax.servlet 包中。那 3 个接口分别是 Filter 、 FilterChain 和 FilterConfig 。从编程的角度看,过滤器类将实现 Filter 接口,然后使用这个过滤器类中的 FilterChain 和 FilterConfig 接口。该过滤器类的一个引用将传递给 FilterChain 对象,以允许过滤器把控制权传递给链中的下一个资源。 FilterConfig 对象将由容器提供给过滤器,以允许访问该过滤器的初始化数据。
为了与我们的三步模式保持一致,过滤器必须运用三个方法,以便完全实现 Filter 接口:
init() :这个方法在容器实例化过滤器时被调用,它主要设计用于使过滤器为处理做准备。该方法接受一个 FilterConfig 类型的对象作为输入。
doFilter() :与 servlet 拥有一个 service() 方法(这个方法又调用 doPost() 或者 doGet() )来处理请求一样,过滤器拥有单个用于处理请求和响应的方法?D?D doFilter() 。这个方法接受三个输入参数:一个 ServletRequest 、 response 和一个 FilterChain 对象。
destroy() :正如您想像的那样,这个方法执行任何清理操作,这些操作可能需要在自动垃圾收集之前进行。
SessionFilter.java
package net.pms.web.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; /** * @author jfish * @since 2006.1.12 */ public class SessionFilter implements Filter { public static boolean isContains(String container, String[] regx) { boolean result = false; for (int i = 0; i < regx.length; i++) { if (container.indexOf(regx[i]) != -1) { return true; } } return result; } public FilterConfig config; public void setFilterConfig(FilterConfig config) { this.config = config; } public FilterConfig getFilterConfig() { return config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpreq = (HttpServletRequest) request; HttpServletResponse httpres = (HttpServletResponse) response; HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper( (HttpServletResponse) response); String logonStrings = config.getInitParameter("logonStrings"); String includeStrings = config.getInitParameter("includeStrings"); String redirectPath = httpreq.getContextPath() + config.getInitParameter("redirectPath"); String disabletestfilter = config.getInitParameter("disabletestfilter"); if (disabletestfilter.toUpperCase().equals("Y")) { chain.doFilter(request, response); return; } String[] logonList = logonStrings.split(";"); String[] includeList = includeStrings.split(";"); Object user = httpreq.getSession().getAttribute("userinfo"); if (user == null) { if (!this.isContains(httpreq.getRequestURI(), includeList)) { chain.doFilter(request, response); return; } if (this.isContains(httpreq.getRequestURI(), logonList)) { chain.doFilter(request, response); return; } wrapper.sendRedirect(redirectPath); } else { chain.doFilter(request, response); } } public void destroy() { this.config = null; } public void init(FilterConfig filterConfig) throws ServletException { this.config = filterConfig; } }
4.配置 Servlet 过滤器
在web.xml中:
<filter> <filter-name>SessionFilter</filter-name> <filter-class>net.pms.web.filter.SessionFilter</filter-class> <init-param> <param-name>logonStrings</param-name> <param-value>login.jsp</param-value> </init-param> <init-param> <param-name>includeStrings</param-name> <param-value>.jsp;.html</param-value> </init-param> <init-param> <param-name>redirectPath</param-name> <param-value>/login.jsp</param-value> </init-param> <init-param> <param-name>disabletestfilter</param-name> <param-value>N</param-value> </init-param> </filter> <filter-mapping> <filter-name>SessionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
其中参数logonStrings,登陆页面
includeStrings,过滤页面参数
redirectPath,没有登陆转向页面
disabletestfilter,过滤器是否有效。