Java 中filter 过滤器的使用: Servlets Filter 是Servlet 2
3 规范中新增加的,它是截取用户从客户端提交的请求,在还没有到达需要访问的资源时运行的一个类
它操纵来自客户端的请求,在资源还没有初发送到客户端前截取响应,并处理这些还没有发送到客户端的响应
Filters 有许多的应用场合
Servlet 2
3 规范建议如下的地方可以应用Filter: authentication filters logging and auditing filters image conversion filters data compression filters encryption filters tokenizing filters filters that trigger resource access events XSL/T filters that transform XML content MIME-type chain filters 如何实现一个过滤器呢
所在的类实现 Filter 接口 public interface Filter public void init(FilterConfig filterConfig)throws ServletException 过滤器初始化是在容器启动时自动初始化 public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletExcepton 在标准Servlet 中的doGet 或doPost 方法中,传递参数为:HttpServletRequest、HttpServletResponse 过滤器销毁 public void