过滤器

一个简单的过滤器:
1.配置web.xml
  <filter>
    <filter-name>test_filter</filter-name>
    <filter-class>cn.fcz.test.filter.FilterTest</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>test_filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
2.写一个类继承Filter 接口
  public class FilterTest implements Filter {
    ServletContext servletContext ;
    public void destroy() {
    }
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
      FilterChain arg2) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)arg0 ;
        HttpServletResponse httpServletResponse = (HttpServletResponse)arg1 ;
        String path = httpServletRequest.getRequestURI();
        System.out.println(path);
        arg2.doFilter(arg0, arg1);
    }
    public void init(FilterConfig arg0) throws ServletException {
      servletContext = arg0.getServletContext() ;
    }
  }

3. 随便找个jsp页面测试即可。

posted @ 2012-09-19 18:39  coreWars  阅读(114)  评论(0)    收藏  举报