I. garbled characters on the JSP page
2. garbled characters appear when the form is submitted to Chinese
Iii. Database Connection
During the JSP development process, Chinese garbled characters often occur, which may affect you. Now I have encountered
For your reference.
I. garbled characters on the JSP page
The following display page (display. jsp) is garbled:
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<%
Out. Print ("JSP Chinese processing ");
%>
</Body>
</Html>
Different web servers and JDK versions have different processing results. Cause: the server uses different encoding methods and browsers.
Result of different display results for different characters. Solution: Specify the encoding method (gb2312) on the JSP page, that is, the first
Add <% @ page contenttype = "text/html; charset = gb2312" %> to remove garbled characters. The complete page is as follows:
:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<%
Out. Print ("JSP Chinese processing ");
%>
</Body>
</Html>
2. garbled characters appear when the form is submitted to Chinese
The following is a submission page (submit. jsp). The Code is as follows:
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<Form name = "form1" method = "Post" Action = "process. jsp">
<Div align = "center">
<Input type = "text" name = "name">
<Input type = "Submit" name = "Submit" value = "Submit">
</Div>
</Form>
</Body>
</Html>
The following is the process. JSP code:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<% = Request. getparameter ("name") %>
</Body>
</Html>
If the English characters submitted by submit. jsp are correctly displayed, garbled characters will appear when you submit Chinese characters. Cause: the browser uses UTF by default.
-8 encoding is used to send requests, while UTF-8 and gb2312 encoding are different in character encoding, which leads to unrecognized characters.
Solution: request. setcharacterencoding ("gb2312") is used to uniformly encode the request.
Display. The modified process. JSP code is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
Request. setcharacterencoding ("gb2312 ");
%>
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<% = Request. getparameter ("name") %>
</Body>
</Html>
3. garbled database connection
As long as all Chinese characters are garbled, the solution is to add
Useunicode = true & characterencoding = GBK is OK.
4. garbled display of databases
In mysql4.1.0, Chinese characters are garbled in the varchar and text types. For the varchar type, set it to the binary attribute.
It can solve the Chinese problem. For the text type, an encoding and conversion class should be used for processing. The implementation is as follows:
Public class convert {
/** Convert the ISO-8859-1 code to gb2312
*/
Public static string isotogb (string ISO ){
String GB;
Try {
If (ISO. Equals ("") | ISO = NULL ){
Return "";
}
Else {
ISO = ISO. Trim ();
GB = new string (ISO. getbytes ("ISO-8859-1"), "gb2312 ");
Return GB;
}
}
Catch (exception e ){
System. Err. Print ("encoding conversion error:" + E. getmessage ());
Return "";
}
}
}
By compiling it into a class, you can call the static method isotogb () of the convert class to convert the encoding.
Summary:
1. In JSP, if <% @ page contenttype = "text/html; charset = A" %> is specified, all the structures in JSP are modified.
If no encoding is specified, the encoding of these strings is.
The string obtained from the request is a iso-8859-1 if no request encoding is specified.
The string obtained from other places uses the original initial encoding, for example, the string obtained from the database. If the database Encoding
If it is B, the string is encoded as B rather than a, and is not the default value.
At this time, if the encoding of the string to be output is not a, it is likely to display garbled characters. Therefore, you must first convert the string into a correct one.
It is a string encoded with a and then output.
2. In JSP, <% @ page contenttype = "text/html; charset = A" %> is not specified, it is equivalent to specifying <% @
Page contenttype = "text/html; charset = ISO-8859-1" %>
3. If response. setcontenttype ("text/html; charset = A") is executed in servelte
Character output stream encoding is set to A. All string codes to be output must be converted to a. Otherwise, garbled characters are obtained.
The string code obtained from the request in servelet is the same as that in JSP, but
String is the default encoding of the system. The string obtained from the external in the servelt uses the original encoding, such
The data obtained from a database whose code is B is encoded as B, not a, or the default encoding of the system.