JSP is a very hot topic, but most people have headache is the garbled problem in the JSP page. The author has an experience; in order to solve the Chinese garbled problem, it is a few days. I don't do it. Finally, with the help of JSP professionals, finally resolved. The author has been a small study on this.
First we first understand the causes of the problem. The general situation has such a statement in the head of each JSP page:
<% Page contentType = "text / html; charset = GB2312"%> This statement determines this page using GB2312 coding form, and in the database is ISO-8859-1 character set storage data. And Java programs are When processing characters, the default ISO-8859-1 character set (reflecting Java International Thought), so the default character set encoding is ISO-8859-1 when adding data, and the page is GB2312, so it will appear Garbled problem. To solve this problem, the GB2312 should be changed to ISO-8859-1 when storing. There is also a garbled when reading it, then it is possible to convert ISO-8859-1 into GB2312.
Specific instances summarize the following points, may have some help to everyone.
1. When establishing a JSP page, you should pay attention to the head of the JSP page to join the code.
<% Page contentType = "text / html; charset = GB2312"%>
This method is to solve garbled when the JSP page is displayed.
2, sometimes add this sentence in
head> in the HTML code3, use ISO-8859-1 encoding in Bean, in JSP, generally encoded GB2312, processing such garbled problems as follows
String str = new string (Strname.GetBytes ("ISO-8859-1"), "GB2312");
For details to this method, when submitting forms to bean (BEAN's function is to store data to the database) form commit, the data stored in the database is indeed? ? ? ? Therefore, the encoding is converted before calling the bean, the method: string str = new string (Request.getParameter (). GetBytes ("ISO-8859-1"), "GB2312");
In this way, the data of the database stored in the database is visible.
4. Parameter delivery between the page can also be used to handle garble questions:
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, add this attribute when the form is defined, sometimes it can also solve the garbled code that appears during the form of form.