您的位置:首页 > 运维架构 > Tomcat

tomcat的server.xml中的UrIEncoding编码设置

2014-09-22 16:47 85 查看
如果设置了其为中文编码格式,只对get方法有效,但是post方法就不可以了,因为这个属性本来就是设置了get时候的数据编码,如何使它对post也有作用呢?

引用

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

官方文档中的说明,该编码指定的是为URI进行解码用的,只对GET请求有效,POST请求参数是以http body形式提交的,不会受此影响。如果需要对POST指定编码,你可以使用EncodingFilter这样是实现,比如spring就提供了一个。

Java代码


<filter>

<filter-name>characterEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf8</param-value>

</init-param>

</filter>

放在所有filter的第一个就好了

post数据只需要在截取或者过滤器中实现编码转换一般就不会有问题,而get,通过地址栏提交,这种方式在本质上就容易出现编码问题。这个其实也是为什么我们经常说post提交数据安全完整的原因。你看UrIEncoding这个属性的意思就是UrI的编码,刚好是针对get这种地址参数的。所以基本上不用考虑对post起作用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: