I am going to start using TOMCAT8, first solve the problem of Chinese garbled, in solving other problems!
Personal Recommendation: Modify the Server.xml way
For the SPRINGMVC, I'll add the question later.
1. Description of the problem
After the Tomcat 7 project switches to Tomcat 8, garbled characters appear. Whether Google or Baidu, most of the solution is Server.xml set uriencoding= "UTF-8", this configuration in order to solve the GET request of the Chinese garbled problem. The configuration is correct for Tomcat 7, but it is not valid for "Tomcat 7 normal, switch to Tomcat 8" garbled.
2. Solution [personal dislike]
TOMCAT8 the Server.xml Configuration Connector node adds properties uriencoding= "Iso-8859-1".
Reference: http://tomcat.apache.org/migration-8.html
3. Official Document Interpretation
Https://tomcat.apache.org/tomcat-7.0-doc/config/http.html
Uriencoding
This specifies the character encoding used to decode the URI of bytes, after%xx decoding the URL. If not specified, iso-8859-1 'll be used.
Https://tomcat.apache.org/tomcat-8.0-doc/config/http.html
Uriencoding
This specifies the character encoding used to decode the URI of bytes, after%xx decoding the URL. If not specified, UTF-8 'll be used unless the Org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property was set to TR UE in which case iso-8859-1 would be used.
TOMCAT7 default encoding for URIs is iso-8859-1
Tomcat8 default encoding for URIs is UTF-8
4. About the coding problem 4.1 Tomcat7 This URI default encoding brings a lot of problems, this should be common:
new String(value.getBytes("ISO-8859-1"), param);
If the Server.xml is configured on uriencoding= "UTF-8" it is not required. Then the project is migrated directly to the TOMCAT8, without modifying the server.xml, or adding uriencoding= "UTF-8" is not a problem.
4.2 Tomcat8 is not because of the developer server transcoding trouble, the URI default encoding changed to "UTF-8".
For the TOMCAT8 development project, it is much simpler and does not require the above-mentioned cumbersome code. But the project migrated from TOMCAT7, either, remove the code above, or server.xml add uriencoding= "iso-8859-1", wasting Tomcat8 a kindness.
4.3 Of course, by judging whether the parameter value is garbled, encoding is also very good
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChineseUtill {
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
public static boolean isMessyCode(String strName) {
Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
Matcher m = p.matcher(strName);
String after = m.replaceAll("");
String temp = after.replaceAll("\\p{P}", "");
char[] ch = temp.trim().toCharArray();
float chLength = 0 ;
float count = 0;
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (!Character.isLetterOrDigit(c)) {
if (!isChinese(c)) {
count = count + 1;
}
chLength++;
}
}
float result = count / chLength ;
if (result > 0.4) {
return true;
} else {
return false;
}
}
public static String toChinese(String msg){
if(isMessyCode(msg)){
try {
return new String(msg.getBytes("ISO8859-1"), "UTF-8");
} catch (Exception e) {
}
}
return msg ;
}
}
From for notes (Wiz)
TOMCAT7 Project migrated to Tomcat8 Chinese garbled problem