Small discussion on JSP Chinese garbled characters
Source: Internet
Author: User
Small discussion on JSP Chinese garbled characters
------- Guo Peng
JSP is a hot topic, but most people have a headache about garbled code on JSP pages. I have some experience. I have been depressed for several days to solve Chinese Garbled text, I tried a lot of methods. Finally, with the help of JSP professionals, we finally solved the problem. Later, I made some small research on this.
First, let's take a look at the cause of the problem. Generally, there is a statement in the header of each JSP page:
<% PAGE contenttype = "text/html; charset = gb2312" %> This statement determines that the page uses the gb2312 encoding format, while the database generally uses the iso-8859-1 character set to store data. java program in the processing of characters by default using the unified ISO-8859-1 character set (reflect Java internationalization idea), so when adding data, the default character set encoding is iso-8859-1, and the page is gb2312, so there is a garbled problem. To solve this problem, we should replace gb2312 into a iso-8859-1 during storage. At this time in the read will also appear garbled, then only the reverse can be, the iso-8859-1 into gb2312.
The following examples may be helpful to you.
1. When creating a JSP page, add the code to the header of the JSP page.
<% PAGE contenttype = "text/html; charset = gb2312" %>
This method solves the garbled characters when the JSP page is displayed.
2. Sometimes you need to add this sentence to <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
3. iso-8859-1 encoding is used in bean, and gb2312 encoding is generally used in JSP to handle such garbled characters as follows:
String STR = new string (strname. getbytes ("iso-8859-1"), "gb2312 ");
Specifically, when the form is submitted to Bean (the bean function is to store data to the database), the data stored in the database is actually changed ????, Therefore, the encoding should be converted before the bean is called by string STR = new string (request. getparameter (). getbytes ("iso-8859-1"), "gb2312 ");
In this way, the data stored in the database is visible.
4. You can also use this method to handle Garbled text when passing parameters between pages:
Public String getstr (string Str)
{
Try
{
String temp_p = STR;
Byte [] temp_t = temp_p.getbytes ("ISO8859-1 ");
String temp = new string (temp_t );
Return temp;
}
Catch (exception E)
{}
Return "null ";
}
5. Adding this attribute to the form definition can also solve the garbled characters in the form submission process.
<Form enctype = "multipart/form-Data"> </form>
6. The English characters to be submitted can be correctly displayed. garbled characters will appear when the Chinese characters are submitted. Cause: by default, the browser uses the UTF-8 encoding method to send requests, while the UTF-8 and gb2312 encoding method to indicate the character is not the same, so there is an unrecognized character. Solution: request. setcharacterencoding ("gb2312") is used to uniformly encode the request, and the normal display of Chinese characters is realized.
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.