昨天遇到了一个问题,项目中需要用到了urlrewrite,发现地址经过rewrite后,接收到表单post过来的中文数据全是乱码,奇怪了,我的Set Character Encoding这个filter为啥没起作用呢,如下示例,找了半天原因,后来网上找到一篇文章,简单说是filter加载是有顺序的,请看第二个xml,将设置编码方式的filter放到了urlrewrite前面,乱码就不见了,我也不知道具体是为什么,但是通过断点发现这个web.xml中确实是按照你写的先后顺序来执行的,估计是urlrewrite中有转过码,导致filter中再设置编码已经无效。
web.xml示例(接收post中文参数乱码) <!-- Url Rewrite Filter --> <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>logLevel</param-name> <param-value>INFO</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Set Character Encoding --> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
web.xml示例(接收post中文参数没有乱码) <!-- Set Character Encoding --> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Url Rewrite Filter --> <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>logLevel</param-name> <param-value>INFO</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
1 楼
iq527
2010-12-28
看到这个问题顺便翻了下servlet specification
其中是这样描述的:
也就是说filter chain
1.首先是根据web.xml中filter的url-pattern的先后顺序来创建
2.然后再按照filter配置中的servlet-name的先后顺序
如果有filter同时配置了url-pattern, servlet-name的话, 则会将该filter mapping拆分成多个filter mapping,同时遵守前面的两条准则
其中是这样描述的:
The order the container uses in building the chain of filters to be applied for a particular request URI is as follows: 1. First, the <url-pattern> matching filter mappings in the same order that these elements appear in the deployment descriptor. 2. Next, the <servlet-name> matching filter mappings in the same order that these elements appear in the deployment descriptor. If a filter mapping contains both <servlet-name> and <url-pattern>, the container must expand the filter mapping into multiple filter mappings (one for each <servlet-name> and <url-pattern>), preserving the order of the <servlet- name> and <url-pattern> elements.
也就是说filter chain
1.首先是根据web.xml中filter的url-pattern的先后顺序来创建
2.然后再按照filter配置中的servlet-name的先后顺序
如果有filter同时配置了url-pattern, servlet-name的话, 则会将该filter mapping拆分成多个filter mapping,同时遵守前面的两条准则