The following provides 4 different solutions to garbled characters, which basically cover the different situations in which garbled characters appear in web development.
The garbled code solution is mainly proposed for the java+mysql development environment. As long as the corresponding modifications are made, it can be used to solve the garbled code problem in different language environments.
1. The JSP page displays garbled characters
The following display page (display.jsp) appears garbled:
program code
<html>
<head>
<title>Chinese processing of JSP</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
out.print("JSP Chinese processing");
%>
</body>
</html>
For different WEB servers and different JDK versions, the processing results are different. Reason: The encoding method used by the server is different and the browser displays different results for different characters. Solution: Specify the encoding method (gb2312) in the JSP page, that is, add: <%@ page contentType="text/html; charset=gb2312"%> to the first line of the page to eliminate garbled characters. The complete page is as follows:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>Chinese processing of JSP</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 in Chinese. Below is a submission page (submit.jsp) with the following code:
Program code
<html>
<head>
<title>Chinese processing of JSP</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 processing page (process.jsp) code:
Program code
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>Chinese processing of JSP</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%=request.getParameter("name")%>
</body>
</html>
If the English characters submitted in submit.jsp can be displayed correctly, if the Chinese characters are submitted, garbled characters will appear. Reason: The browser uses UTF-8 encoding by default to send requests, and UTF-8 and GB2312 encoding methods represent characters differently, so characters cannot be recognized. Solution: Uniformly encode the request through request.seCharacterEncoding("gb2312") to achieve normal display of Chinese. The modified process.jsp code is as follows:
Program code
<%@ page contentType="text/html; charset=gb2312"%>
<%
request.seCharacterEncoding("gb2312");
%>
<html>
<head>
<title>Chinese processing of JSP</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%=request.getParameter("name")%>
</body>
</html>
3. The database connection is garbled.
As long as everything involving Chinese is garbled, the solution is to add useUnicode=true&characterEncoding=GBK to the database URL of the database and it will be OK.
4. Database display is garbled
In mysql4.1.0, Chinese garbled characters will appear in the varchar type and text type. For the varchar type, setting it as a binary attribute can solve the Chinese problem. For the text type, an encoding conversion class must be used to handle it. The implementation is as follows:
Program code
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("Encoding conversion error:"+e.getMessage());
return "";
}
}
}
Compile it into a class, and you can call the static method ISOtoGB() of the Convert class to convert the encoding.