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