Solve jsp Chinese garbled characters

Source: Internet
Author: User
Tags character set java web

 

In jsp, when out. print (request. getParameter ("parameter-name") when outputting form data, if the form data is a Chinese character, the output content is garbled.

The reason for Chinese garbled characters is that, by default, the encoding format used to extract form data is UTF-8. to solve the problem of Chinese garbled characters, we first need to use the ISO-8859-1 encoding format to store the form data to the byte array, and then use the gb2312 encoding format to convert the byte array to the string. The complete code for processing the form is as follows:

 



String str = request. getParameter ("parameter-name ");

Byte [] buffer = str. getByte ("ISO-8859-1 ");

String str1 = new String (buffer, "GB2312 ");

 

Zhang Xiaoxiang's "Java web development insider-core basics" provides a detailed explanation of Chinese garbled text. It is recommended that you take a good look (in addition, Sun Xin's Java teaching video also provides a detailed explanation of garbled text ). On the p356 page of the book, it is described as follows:

[Because the getParameter method converts the originally gb2312 encoded request parameter value into an incorrect unicode string according to the ISO8859-1 character set encoding, printWriter. the Chinese garbled characters occur when the println method encodes the incorrect unicode string into an array based on the gb2312 character set.

The conversion from ISO8859-1 character set encoding to unicode encoding is a reversible operation without information loss (for the principle, refer to Sun Xin's Java video teaching ), the original byte array can be restored as long as the unicode string is converted to ISO8859-1 encoding.

Then, we will convert the restored initial byte array to unicode based on its original character set encoding to obtain the correct unicode string. Then output the correct unicode according to a correct character set encoding. Then, the browser can display the correct Chinese characters .]

For specific methods, refer to the above code

Keywords: solve jsp Chinese garbled problem jsp Chinese display garbled jsp page garbled
Jsp form submission garbled jsp page display garbled Chinese parameters pass garbled jsp parameters Chinese garbled jsp passing parameters jsp passing Chinese parameters jsp passing multiple parameters
Url Chinese parameter garbled jsp Chinese garbled jsp garbled problem
  
 

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.