Tomcat7下项目切换到Tomcat8后,出现乱码。
无论Google还是百度,多数解决方法是server.xml设置URIEncoding=“UTF-8”。
对于Tomcat7下遇到乱码问题,这样配置是正确的;但是对Tomcat7正常,切换到Tomcat8乱码的情况无效。
Tomcat8的server.xml配置添加为URIEncoding=“ISO-8859-1”。
参考:官方迁移说明
https://tomcat.apache.org/tomcat-7.0-doc/config/http.html
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.
https://tomcat.apache.org/tomcat-8.0-doc/config/http.html
URIEncoding
This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.
Tomcat7对URI默认编码是ISO-8859-1
Tomcat8对URI默认编码是UTF-8
针对我的问题,我用的是tomcat8的服务器,而项目外部环境使用的是tomcat7,所以我要把自己的tomcat8的服务器默认编码变为ISO-8859-1,如下图所示:
>new String(value.getBytes(“ISO-8859-1”), param);
如果server.xml配置上URIEncoding=“UTF-8”就不需要了。
进而项目直接迁移到Tomcat8,不修改server.xml,或者再次加上URIEncoding=“UTF-8”也是不会有问题。
对于在Tomcat8开发项目,就简单很多,不需要上面的那段不人道的代码。但是从Tomcat7迁移上来的项目,要么,去掉上面的代码,要么server.xml添加URIEncoding=“ISO-8859-1”,浪费Tomcat8一番美意。
http://my.codeweblog.com/cevin15/blog/402975