Today, I was hit by a JSP garbled problem. The solution is as follows:
1. The TOMCAT6.0.33 configuration file server. xml does not contain URIEncoding = "UTF-8"
The URL has a Chinese string and java is used in the background,
String str = request. getParameter ("hello ");
System. out. println (str );
The result is garbled and changed
System. out. println (new String (str. getBytes ("iso-8859-1"), "UTF-8 "));
The result is normal.
2. The TOMCAT configuration file server. xml has URIEncoding = "UTF-8", such:
<Connector port = "8080" protocol = "HTTP/1.1"
ConnectionTimeout = "20000"
RedirectPort = "8443" URIEncoding = "UTF-8"/>
The URL has a Chinese string and java is used in the background,
String str = request. getParameter ("hello ");
System. out. println (new String (str. getBytes ("iso-8859-1"), "UTF-8 "));
It is also garbled because the container has already specified URIEncoding = "UTF-8". Do not convert it again !!!
Change to System. out. println (str); the result is normal.
3. No matter which local character set is used for the character, the Unicode Character Set is represented as the same encoding. Or, the Unicode Character Set is irrelevant to the language type.
The process of using two encodings is equivalent to the following code:
String name = java.net. URLEncoder. encode ("test", "UTF-8 ");
System. out. println (name );
Name = java.net. URLEncoder. encode (name, "UTF-8 ");
System. out. println (name );
Name = java.net. URLDecoder. decode (name, "UTF-8 ");
System. out. println (name );
System. out. println (java.net. URLDecoder. decode (name, "UTF-8 "));
Output:
% E6 % B5 % 8B % E8 % AF % 95
% 25E6% 25B5% 258B % 25E8% 25 af % 2595
% E6 % B5 % 8B % E8 % AF % 95
Test
After the first encoding, the Chinese characters are encoded in the format of % and letters and numbers, while the second encoding is to encode % letters and numbers, although the decoding is the ISO-8859-1, but for % and alphanumeric characters decoded with ISO-8859-1 and UTF-8 is the same, then return to the Chinese character is encoded once the string, when decoding again, use the UTF-8 to move it back to the Chinese character.
From Mingzhi Studio