Tomcat7下項目切換到Tomcat8後,出現亂碼。html
不管Google仍是百度,多數解決方法是server.xml設置URIEncoding="UTF-8"。apache
對於Tomcat7下遇到亂碼問題,這樣配置是正確的;可是對」Tomcat7正常,切換到Tomcat8」亂碼的狀況無效。tomcat
Tomcat8的server.xml配置Connector節點添加屬性URIEncoding="ISO-8859-1"。less
參考:官方遷移說明編碼
https://tomcat.apache.org/tomcat-7.0-doc/config/http.html.net
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.code
https://tomcat.apache.org/tomcat-8.0-doc/config/http.htmlserver
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.xml
Tomcat7對URI默認編碼是ISO-8859-1htm
Tomcat8對URI默認編碼是UTF-8
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一番美意。