huangfox

冰冻三尺,非一日之寒!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

(一)servlet过滤器的概念

Servlet 过滤器是小型的 Web 组件,它们拦截请求和响应,以便查看、提取或以某种方式操作正在客户机和服务器之间交换的数据。过滤器是通常封装了一些功能的 Web 组件,这些功能虽然很重要,但是对于处理客户机请求或发送响应来说不是决定性的。典型的例子包括记录关于请求和响应的数据、处理安全协议、管理会话属性,等等。过滤器提供一种面向对象的模块化机制,用以将公共任务封装到可插入的组件中,这些组件通过一个配置文件来声明,并动态地处理。
 

(二)servlet的应用场景

A.认证过滤
B.登录和审核过滤
C.图像转换过滤 
D.数据压缩过滤 
E.加密过滤 
F.令牌过滤 
G.资源访问触发事件过滤 
H.XSL/T过滤 
I.Mime-type过滤


(三)servlet过滤器API

1.Interface Filter

Method Summary
 void destroy()
          Called by the web container to indicate to a filter that it is being taken out of service.
 void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
          The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
 void init(FilterConfig filterConfig)
          Called by the web container to indicate to a filter that it is being placed into service.

2.Interface FilterConfig

Method Summary
 java.lang.String getFilterName()
          Returns the filter-name of this filter as defined in the deployment descriptor.
 java.lang.String getInitParameter(java.lang.String name)
          Returns a String containing the value of the named initialization parameter, or null if the initialization parameter does not exist.
 java.util.Enumeration<java.lang.String> getInitParameterNames()
          Returns the names of the filter's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the filter has no initialization parameters.
 ServletContext getServletContext()
          Returns a reference to the ServletContext in which the caller is executing.

3.Interface FilterChain

Method Summary
 void doFilter(ServletRequest request, ServletResponse response)
          Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked.

4.Class ServletRequestWrapper

Provides a convenient implementation of the ServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object. 

5.Class ServletResponseWrapper

Provides a convenient implementation of the ServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped response object. 

6.Class HttpServletRequestWrapper

7.Class HttpServletResponseWrapper
 

(四)servlet过滤器的部署

过滤器的部署是通过filter和filter-mapping元素来定义的。

filter包括:description、display-name、icon、filter-name、filter-class、init-param(description、param-name、param-value)

注意:过滤器和servlet类似,容器只会创建一个实例,所以必须注意线程安全问题。

filter-mapping包括:filter-name、url-pattern或servlet-name、dispatcher。

注意:dispatcher包括:REQUEST、INCLUDE、FORWARD、ERROR。filter-mapping元素可以包括0-4个dispatcher元素,它指定过滤器

对应的请求方法。默认为REQUEST。

 

(五)对请求或者响应进行处理的过滤器

与请求或者响应相关的过滤器包括:

ServletRequestWrapper

ServletResponseWrapper

HttpServletRequestWrapper

HttpServletResponseWrapper

以上四个都是装饰类,装饰类是装饰设计模式的运用,它提供了一种不使用继承而修改或增加现有对象功能的方法。



-------------------------------------------

过滤器实例:

Servlet过滤器大全http://www.blogjava.net/zzheng/archive/2008/09/05/227305.html

posted on 2011-10-24 16:01  huangfox  阅读(1028)  评论(0编辑  收藏  举报