
http://liuluo129.iteye.com/blog/1965268
图来自上面连接
http://blog.csdn.net/geloin/article/details/7441330
整理于上面
Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所当然地实现了javax.servlet.Filter,不过实现的方式有以下几类:
GenericFilterBean
- /**
- publicfinalvoidthrows
- if
- "'"
- this
- try
- newthis
- this
- new
- classnewthis
- true
- catch
- thrownew
- if
- newthis
- this
- new
- bw.registerCustomEditor(Resource.classnewthis
- true
OncePerRequestFilter
- /**
- publicfinalvoid
- throws
- ifinstanceofinstanceof
- thrownew
- ifnull
- else
- try
- finally
AbstractRequestLoggingFilter
- /**
- @Override
- protectedvoid
- throws
- if
- new
- try
- finally
区别
AbstractRequestLoggingFilter继承自OncePerRequestFilter,
OncePerRequestFilter继承自GenericFilterBean,
所以我们知道,genericFilterBean是任何类型的过滤器的一个比较方便的超类,
这个类主要实现的就是从web.xml文件中取得init-param中设定的值,然后对Filter进行初始化(当然,其子类可以覆盖init方法)。
OncePerRequestFilter继承自GenericFilterBean,那么它自然知道怎么去获取配置文件中的属性及其值,所以其重点不在于取值,而在于确保在接收到一个request后,每个filter只执行一次,它的子类只需要关注Filter的具体实现即doFilterInternal。
AbstractRequestLoggingFilter是对OncePerRequestFilter的扩展,它除了遗传了其父类及祖先类的所有功能外,还在doFilterInternal中决定了在过滤之前和之后执行的事件,它的子类关注的是beforeRequest和afterRequest。
这三个类分别执行了Filter的某部分功能,当然,具体如何执行由它们的子类规定,若你需要实现自己的过滤器,也可以根据上文所述继承你所需要的类
==============================================================================================================
Spring MVC过滤器-上下文信息过滤器(AbstractRequestLoggingFilter及其子类)
http://blog.csdn.net/geloin/article/details/7443329
两个方法beforeRequest和afterRequest分别用于设定过滤前后执行的操作,
它有三个子类,
分别是CommonsRequestLoggingFilter、
ServletContextRequestLoggingFilter
和Log4jNestedDiagnosticContextFilter,
这三个子类分别实现了各自的beforeRequest和afterRequest。其中,
CommonsRequestLoggingFilter在过滤前后分别打印出一段debug的信息;
ServletContextRequestLoggingFilter在过滤前后分别向日志文件中写入一段日志信息,日志文件可由log4j.properties等指定;
Log4jNestedDiagnosticContextFilter则将日志信息存储到NDC中,NDC采用了一个类似栈的机制来push和pot上下文信息,每一个线程都独立地储存上下文信息,比如说一个servlet就可以针对 每一个request创建对应的NDC,储存客户端地址等信息。
日志信息在getBeforeMessage和getAfterMessage方法中通过createMessage创建,然后被doFilterInternal调用,
日志信息中至少包含uri信息,其他信息则根据init-param中所设定的值获取,
例如,当includeQueryString的值为true时,日志信息中除uri信息外,还包含query string信息。
只不过承载的方式不同,
其中CommonsRequestLoggingFilter将上下文信息直接打印;
ServletContextRequestLoggingFilter将上下文信息写入日志文件;
Log4jNestedDiagnosticContextFilter将上下文信息写入NDC中
==============================================================================================================
Spring MVC过滤器-HiddenHttpMethodFilter
http://blog.csdn.net/geloin/article/details/7444321
- <formaction="..."method="<strong>post</strong>">
- <inputtype="hidden"name="_method"value="<strong>put</strong>"/>
- </form>
而不是使用:
- <formaction="..."method="put">
- </form>
- <filter>
- <filter-name>HiddenHttpMethodFilter</filter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>HiddenHttpMethodFilter</filter-name>
- <servlet-name>spring</servlet-name>
- </filter-mapping>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:dispatcher.xml</param-value>
- </init-param>
- lt;/servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.html</url-pattern>
- lt;/servlet-mapping>
同样的,作为Filter,可以在web.xml中配置HiddenHttpMethodFilter的参数,可配置的参数为methodParam,
default的值为 “_method”,而他在hidden里对应的value为http method的任意一个即可
- <inputtype="hidden"name="_method"value="<strong>put</strong>"/>
- <inputtype="hidden"name="abcd_abc"value="<strong>put</strong>"/>
==============================================================================================================
http://blog.csdn.net/geloin/article/details/7444590
- <filter>
- <filter-name>httpPutFormcontentFilter</filter-name>
- <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>httpPutFormContentFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
需要注意的是,该过滤器只能接受enctype值为application/x-www-form-urlencoded的表单,也就是说,在使用该过滤器时,form表单的代码必须如下
- <formaction=""method="put"enctype="<strong>application/x-www-form-urlencoded</strong>">
- </form>
==============================================================================================================
ShallowEtagHeaderFilter
ShallowEtagHeaderFilter是spring提供的支持ETag的一个过滤器,所谓ETag是指被请求变量的实体值,是一个可以与Web资源关联的记号,而Web资源可以是一个Web页,也可以是JSON或XML文档,服务器单独负责判断记号是什么及其含义,并在HTTP响应头中将其传送到客户端,以下是服务器端返回的格式:
客户端的查询更新格式是这样的:
查看ShallowEtagHeaderFilter的源码如下:
- protectedvoidthrows
- newnull
- byte
- int
- if
- if
- ifthis
- this.logger.trace(
- 304
- else
- ifthis
- this.logger.trace(
- else
- ifthis
- this.logger.trace(
由源码可知,ShallowEtagHeaderFilter只能根据结果判断是否重新向客户端发送数据,并不会不处理请求,因此节省带宽,而不能提高服务器性能。
配置ShallowEtagHeaderFilter的代码如下:
- <filter>
- <filter-name>shallowEtagHeaderFilter</filter-name>
- <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</fliter-class>
- </filter>
- <filter-mapping>
- <filter-name>shallowEtagHeaderFilter</filter-name>
- <servlet-name>spring</servlet-name>
- </filter-mapping>
RequestContextFilter
- <filter>
- <filter-name>RequestContextFilter</filter-name>
- <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>RequestContextFilter</filter-name>
- <servlet-name></servlet-name>
- </filter-mapping>

http://liuluo129.iteye.com/blog/1965268
图来自上面连接
http://blog.csdn.net/geloin/article/details/7441330
整理于上面
Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所当然地实现了javax.servlet.Filter,不过实现的方式有以下几类:
GenericFilterBean
- /**
- publicfinalvoidthrows
- if
- "'"
- this
- try
- newthis
- this
- new
- classnewthis
- true
- catch
- thrownew
- if
- newthis
- this
- new
- bw.registerCustomEditor(Resource.classnewthis
- true
OncePerRequestFilter
- /**
- publicfinalvoid
- throws
- ifinstanceofinstanceof
- thrownew
- ifnull
- else
- try
- finally
AbstractRequestLoggingFilter
- /**
- @Override
- protectedvoid
- throws
- if
- new
- try
- finally
区别
AbstractRequestLoggingFilter继承自OncePerRequestFilter,
OncePerRequestFilter继承自GenericFilterBean,
所以我们知道,genericFilterBean是任何类型的过滤器的一个比较方便的超类,
这个类主要实现的就是从web.xml文件中取得init-param中设定的值,然后对Filter进行初始化(当然,其子类可以覆盖init方法)。
OncePerRequestFilter继承自GenericFilterBean,那么它自然知道怎么去获取配置文件中的属性及其值,所以其重点不在于取值,而在于确保在接收到一个request后,每个filter只执行一次,它的子类只需要关注Filter的具体实现即doFilterInternal。
AbstractRequestLoggingFilter是对OncePerRequestFilter的扩展,它除了遗传了其父类及祖先类的所有功能外,还在doFilterInternal中决定了在过滤之前和之后执行的事件,它的子类关注的是beforeRequest和afterRequest。
这三个类分别执行了Filter的某部分功能,当然,具体如何执行由它们的子类规定,若你需要实现自己的过滤器,也可以根据上文所述继承你所需要的类
==============================================================================================================
Spring MVC过滤器-上下文信息过滤器(AbstractRequestLoggingFilter及其子类)
http://blog.csdn.net/geloin/article/details/7443329
两个方法beforeRequest和afterRequest分别用于设定过滤前后执行的操作,
它有三个子类,
分别是CommonsRequestLoggingFilter、
ServletContextRequestLoggingFilter
和Log4jNestedDiagnosticContextFilter,
这三个子类分别实现了各自的beforeRequest和afterRequest。其中,
CommonsRequestLoggingFilter在过滤前后分别打印出一段debug的信息;
ServletContextRequestLoggingFilter在过滤前后分别向日志文件中写入一段日志信息,日志文件可由log4j.properties等指定;
Log4jNestedDiagnosticContextFilter则将日志信息存储到NDC中,NDC采用了一个类似栈的机制来push和pot上下文信息,每一个线程都独立地储存上下文信息,比如说一个servlet就可以针对 每一个request创建对应的NDC,储存客户端地址等信息。
日志信息在getBeforeMessage和getAfterMessage方法中通过createMessage创建,然后被doFilterInternal调用,
日志信息中至少包含uri信息,其他信息则根据init-param中所设定的值获取,
例如,当includeQueryString的值为true时,日志信息中除uri信息外,还包含query string信息。
只不过承载的方式不同,
其中CommonsRequestLoggingFilter将上下文信息直接打印;
ServletContextRequestLoggingFilter将上下文信息写入日志文件;
Log4jNestedDiagnosticContextFilter将上下文信息写入NDC中
==============================================================================================================
Spring MVC过滤器-HiddenHttpMethodFilter
http://blog.csdn.net/geloin/article/details/7444321
- <formaction="..."method="<strong>post</strong>">
- <inputtype="hidden"name="_method"value="<strong>put</strong>"/>