Garbled jsp or java

Source: Internet
Author: User
Tags alphanumeric characters

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.