GET提交参数乱码问题(apache-tomcat-9.0.0.M19)

来源:互联网 发布:淘宝男模特排名与图片 编辑:程序博客网 时间:2024/05/16 09:36

我用的IDE是myeclipse 2017 ci3   服务器为apache-tomcat-9.0.0.M19


// 在过滤器中判断如果是get提交并转码

if ("GET".equals(methoSubmit)) {
if (value != null && !"".equals(value.trim())) {
//tomcat服务器配置文件server.xml 中没有设置URIEncoding,
value = new String(value.getBytes("ISO-8859-1"), "utf-8");   //转换后就乱码,不转换正常
}
}


百度了很多文章都说tomcat服务器配置文件server.xml 中没有设置URIEncoding,默认为ISO-8859-1。


但如果是这样的话应该是转码后不会乱码才对。后来实在想不出问题出在哪。去官网查了官方文档。在官方文档的FAQ/CharacterEncoding

得到答案。原来tomcat8.0服务器起默认URIEncoding为utf-8编码。

下面部分对于GET提交参数的解释


How do I change how GET parameters are interpreted?

Tomcat will use ISO-8859-1 as the default character encoding of the entire URL, including the query string ("GET parameters") (though see Tomcat 8 notice below).

There are two ways to specify how GET parameters are interpreted:

  1. Set the URIEncoding attribute on the <Connector> element in server.xml to something specific (e.g. URIEncoding="UTF-8").

  2. Set the useBodyEncodingForURI attribute on the <Connector> element in server.xml to true. This will cause the Connector to use the request body's encoding for GET parameters.

In Tomcat 8 starting with 8.0.0 (8.0.0-RC3, to be specific), the default value of URIEncoding attribute on the <Connector> element depends on "strict servlet compliance" setting. The default value (strict compliance is off) of URIEncoding is now UTF-8. If "strict servlet compliance" is enabled, the default value is ISO-8859-1.


0 0
原创粉丝点击