One, JSP page display garbled
Second, the form is submitted in Chinese garbled
Third, database connection
Everyone in the development process of JSP, often appear in Chinese garbled problem, perhaps one to haunt you, I now put me in the development of JSP encountered
The problem of Chinese garbled and the solution to write for everyone's reference.
One, JSP page display garbled
The following display page (display.jsp) appears garbled:
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<%
Out.print ("Chinese Processing of JSP");
%>
</body>
For different Web servers and different JDK versions, the processing results are not the same. Cause: The server uses different encoding methods and the browser
Causes different characters to display the results differently. Workaround: Specify the Encoding Method (GB2312) in the JSP page, that is, on the first page of the
Line Plus: <%@ page contenttype= "text/html; charset=gb2312 "%>, you can eliminate garbled. The full page is as follows
:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<%
Out.print ("Chinese Processing of JSP");
%>
</body>
Second, the form is submitted in Chinese garbled
Here is a submission page (submit.jsp) with the following code:
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<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>
Here is the processing page (process.jsp) code:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<%=request.getparameter ("name")%>
</body>
If the submit.jsp submit English characters can be displayed correctly, if you submit the Chinese language will appear garbled. Cause: The browser uses UTF by default
-8 encoding to send the request, while UTF-8 and GB2312 encode the characters differently, so that unrecognized characters appear.
Workaround: By request.setcharacterencoding ("gb2312") The request is uniformly encoded, it realizes the normal Chinese
Show. The modified process.jsp code is as follows:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%
Request.secharacterencoding ("gb2312");
%>
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<%=request.getparameter ("name")%>
</body>
Third, the database connection garbled
As long as the Chinese-related places are all garbled, the solution: in the database URL of the database plus
The USEUNICODE=TRUE&CHARACTERENCODING=GBK is OK.
Four, the database display garbled
In mysql4.1.0, the varchar type, the text type will appear in Chinese garbled, for the varchar type to set it as a binary property
Can solve the Chinese problem, for the text type is to use an encoding conversion class to handle, the implementation is as follows:
public class Convert {
/** convert 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 ("Code conversion Error:" +e.getmessage ());
Return "";
}
}
}
By compiling it into class, you can call the static method Isotogb () of the Convert class to convert the encoding.