1. When the form is submitted in Chinese, garbled characters appear.
Here is a submission page (submit.jsp) with the following code:
<html>
<head>
<title> JSP's 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>
Here is the processing page (process.jsp) code:
<%@ page contenttype="text/html; charset=gb2312 "%>
<html>
<head>
<title> JSP's Chinese processing </title>
<Meta http-equiv= "content-type" content= "text/html; charset=gb2312 ">
</head>
<body>
<%=request.getparameter ("name")%>
</body>
</html>
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-8 encoding to send the request by default , while UTF-8 and GB2312 encode the characters differently, which makes the unrecognized character appear. Solution: By request.secharacterencoding ("gb2312") the request is uniformly encoded, the normal display of Chinese is realized. The modified process.jsp code is as follows:
<%@ page contenttype="text/html; charset=gb2312 "%>
<%
Request.secharacterencoding ("gb2312");
%>
<html>
<head>
<title> JSP's Chinese processing </title>
<Meta http-equiv= "content-type" content= "text/html; charset=gb2312 ">
</head>
<body>
<%=request.getparameter ("name")%>
</body>
</html>
2, database connection garbled
As long as the Chinese language is all garbled, the solution: in the database URL of the database plus USEUNICODE=TRUE&CHARACTERENCODING=GBK is OK.
3, the database display garbled
In mysql4.1.0, the type of varchar, the text type will appear in Chinese garbled, for the varchar type set it to binary attribute can solve the Chinese problem, for the text type is to use an encoding conversion class to handle, the implementation is as follows:
public string ISO2GB (string qs)
{
try{
if (QS = = null) return "NULL";
Else
{
return new String (Qs.getbytes ("iso-8859-1"), "gb2312");
}
}
catch (Exception e) {
SYSTEM.ERR.PRINTLN ("ISO2GB Error:" +e.getmessage ());
}
return "NULL";
}
public string Gb2iso (string qs)
{
Try
{
if (QS = = null) return "NULL";
else {
return new String (Qs.getbytes ("gb2312"), "iso-8859-1"); }
}
catch (Exception e) {System.err.println ("Gb2iso Error:" +e.getmessage ());}
return "NULL";
}
This article is for reference only.
JSP page garbled problem