What’s the Filter
   Filter也称之为过滤器,它是Servlet技术中比较激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。

   如何使用Filter 创建一个Filter类,实现javax.servlet.Filter接口 实现doFilter()方法,进行拦截 在web.xml中进行配置
   package com.yiyexiaoyuan.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;
   /**
   * Servlet Filter implementation class TestFilter
   */
   //实现Filter接口
   public class TestFilter implements Filter
   {
   /**
   * Default constructor.
   */
   public TestFilter()
   {
   // TODO Auto-generated constructor stub
   }
   /**
   * @see Filter#destroy()
   */
   //Filter结束时候调用
   public void destroy()
   {
   // TODO Auto-generated method stub
   }
   /**
   * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
   */
   //拦截主体代码
   public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException
   {
   //去下一个Filter
   chain.doFilter(request, response);
   }
   /**
   * @see Filter#init(FilterConfig)
   */
   //初始化时候调用
   public void init(FilterConfig fConfig) throws ServletException
   {
   // TODO Auto-generated method stub
   }
   }
   在web.xml中配置
   <filter>
   <display-name>TestFilter</display-name>
   <filter-name>TestFilter</filter-name>
   <filter-class>com.yiyexiaoyuan.filter.TestFilter</filter-class>
   </filter>
   <filter-mapping>
   <filter-name>TestFilter</filter-name>
   <url-pattern>/TestFilter</url-pattern>
   </filter-mapping>
   应用场景 解决中文乱码的Filter
   package com.yiyexiaoyuan.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;
   /**
   * Servlet Filter implementation class PageEncodingFilter
   */
   public class PageEncodingFilter implements Filter
   {
   private String encoding = "UTF-8";
   protected FilterConfig filterConfig;
   public void init(FilterConfig filterConfig) throws ServletException
   {
   this.filterConfig = filterConfig;
   // 本过滤器默认编码是UTF-8,但也可以在web.xml配置文件里设置自己需要的编码
   if (filterConfig.getInitParameter("encoding") != null)
   encoding = filterConfig.getInitParameter("encoding");
   }
   public void doFilter(ServletRequest srequset, ServletResponse sresponse,
   FilterChain filterChain) throws IOException, ServletException
   {
   HttpServletRequest request = (HttpServletRequest) srequset;
   request.setCharacterEncoding(encoding);
   filterChain.doFilter(srequset, sresponse);
   }
   public void destroy()
   {
   this.encoding = null;
   }
   }
   web.xml配置
   <filter>
   <display-name>PageEncodingFilter</display-name>
   <filter-name>PageEncodingFilter</filter-name>
   <filter-class>com.yiyexiaoyuan.filter.PageEncodingFilter</filter-class>
   <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
   </init-param>
   </filter>
   <filter-mapping>
   <filter-name>PageEncodingFilter</filter-name>
   <url-pattern>/servlet/*</url-pattern>
   </filter-mapping>