`
QING____
  • 浏览: 2236412 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Tomcat请求乱码(备忘)

 
阅读更多

   纠结了数日,终于搞定了乱码问题,归结是tomcat配置问题,在tomcat server.xml中有2个参数是配置编码的,一个是URIEncoding,另一个是useBodyEncodingForURI.

 

1. URIEncoding

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.

    参数值为字符集,比如"UTF-8"等,这个参数在对http-get请求时有效,很多时候get请求的乱码起源,就是来自这里.任何http-get请求首先经过tomcat进行编码,编码的字符集就是使用"URIEncoding",编码之后整理成httpServerRequest参数交付给application;如果自己的web application也使用了各种url编码(比如在web.xml中配置Filter等),仍然无法改变tomcat对查询字符串编码的结果.

 

2. useBodyEncodingForURI

This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is  false .

 

    参数值为"true"或"false",表示"是否使用contentType中的encoding方式对URI进行编码";如果为true,且http请求中,在contentType中指定了编码方式,那么将采用contentType的编码方式去解码url之后的查询参数,将忽略URIEncoding的配置.此参数仍然只对http-get请求有效,tomcat无需对post请求进行编码.

    所以在get请求时,如果contentType指定了编码方式,将会带来一定的问题(最大的问题就是,服务器端对查询字符串的编码方式无法统一),最后我决定让 useBodyEncodingForURI 参数保持默认(false):即所有的URL查询字符串的编码采用URIEncoding的参数(UTF-8),服务器端编码保持原来的统一方式.

 

<Connector port="8080" protocol="HTTP/1.1" 
    connectionTimeout="20000"  
    redirectPort="8443" 
    URIEncoding="utf-8"/>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics