JSP頁面顯示亂碼 / 表單提交中文時(shí)出現(xiàn)亂碼 / 數(shù)據(jù)庫連(轉(zhuǎn)載)
大家在JSP的開發(fā)過程中,經(jīng)常出現(xiàn)中文亂碼的問題,可能一至困擾著您,我現(xiàn)在把我在JSP開發(fā)中遇到的中文亂碼的問題及解決辦法寫出來供大家參考。
一、JSP頁面顯示亂碼
下面的顯示頁面(display.jsp)就出現(xiàn)亂碼:
<html>
<head>
<title>JSP的中文處理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
out.print("JSP的中文處理");
%>
</body>
</html>
對不同的WEB服務(wù)器和不同的JDK版本,處理結(jié)果就不一樣。原因:服務(wù)器使用的編碼方式不同和瀏覽器對不同的字符顯示結(jié)果不同而導(dǎo)致的。解決辦法:在JSP頁面中指定編碼方式(gb2312),即在頁面的第一行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除亂碼了。完整頁面如下:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>JSP的中文處理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
out.print("JSP的中文處理");
%>
</body>
</html>
二、表單提交中文時(shí)出現(xiàn)亂碼
下面是一個(gè)提交頁面(submit.jsp),代碼如下:
<html>
<head>
<title>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>
下面是處理頁面(process.jsp)代碼:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>JSP的中文處理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%=request.getParameter("name")%>
</body>
</html>
如果submit.jsp提交英文字符能正確顯示,如果提交中文時(shí)就會出現(xiàn)亂碼。原因:瀏覽器默認(rèn)使用UTF-8編碼方式來發(fā)送請求,而UTF-8和GB2312編碼方式表示字符時(shí)不一樣,這樣就出現(xiàn)了不能識別字符。解決辦法:通過request.seCharacterEncoding("gb2312")對請求進(jìn)行統(tǒng)一編碼,就實(shí)現(xiàn)了中文的正常顯示。修改后的process.jsp代碼如下:
<%@ page contentType="text/html; charset=gb2312"%>
<%
request.seCharacterEncoding("gb2312");
%>
<html>
<head>
<title>JSP的中文處理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%=request.getParameter("name")%>
</body>
</html>
三、數(shù)據(jù)庫連接出現(xiàn)亂碼
只要涉及中文的地方全部是亂碼,解決辦法:在數(shù)據(jù)庫的數(shù)據(jù)庫URL中加上useUnicode=true&characterEncoding=GBK就OK了。
四、數(shù)據(jù)庫的顯示亂碼
在mysql4.1.0中,varchar類型,text類型就會出現(xiàn)中文亂碼,對于varchar類型把它設(shè)為binary屬性就可以解決中文問題,對于text類型就要用一個(gè)編碼轉(zhuǎn)換類來處理,實(shí)現(xiàn)如下:
public class Convert {
/** 把ISO-8859-1碼轉(zhuǎn)換成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("編碼轉(zhuǎn)換錯(cuò)誤:"+e.getMessage());
return "";
}
}
}
把它編譯成class,就可以調(diào)用Convert類的靜態(tài)方法ISOtoGB()來轉(zhuǎn)換編碼。
一、JSP頁面顯示亂碼
下面的顯示頁面(display.jsp)就出現(xiàn)亂碼:
<html>
<head>
<title>JSP的中文處理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
out.print("JSP的中文處理");
%>
</body>
</html>
對不同的WEB服務(wù)器和不同的JDK版本,處理結(jié)果就不一樣。原因:服務(wù)器使用的編碼方式不同和瀏覽器對不同的字符顯示結(jié)果不同而導(dǎo)致的。解決辦法:在JSP頁面中指定編碼方式(gb2312),即在頁面的第一行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除亂碼了。完整頁面如下:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>JSP的中文處理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
out.print("JSP的中文處理");
%>
</body>
</html>
二、表單提交中文時(shí)出現(xiàn)亂碼
下面是一個(gè)提交頁面(submit.jsp),代碼如下:
<html>
<head>
<title>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>
下面是處理頁面(process.jsp)代碼:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>JSP的中文處理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%=request.getParameter("name")%>
</body>
</html>
如果submit.jsp提交英文字符能正確顯示,如果提交中文時(shí)就會出現(xiàn)亂碼。原因:瀏覽器默認(rèn)使用UTF-8編碼方式來發(fā)送請求,而UTF-8和GB2312編碼方式表示字符時(shí)不一樣,這樣就出現(xiàn)了不能識別字符。解決辦法:通過request.seCharacterEncoding("gb2312")對請求進(jìn)行統(tǒng)一編碼,就實(shí)現(xiàn)了中文的正常顯示。修改后的process.jsp代碼如下:
<%@ page contentType="text/html; charset=gb2312"%>
<%
request.seCharacterEncoding("gb2312");
%>
<html>
<head>
<title>JSP的中文處理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%=request.getParameter("name")%>
</body>
</html>
三、數(shù)據(jù)庫連接出現(xiàn)亂碼
只要涉及中文的地方全部是亂碼,解決辦法:在數(shù)據(jù)庫的數(shù)據(jù)庫URL中加上useUnicode=true&characterEncoding=GBK就OK了。
四、數(shù)據(jù)庫的顯示亂碼
在mysql4.1.0中,varchar類型,text類型就會出現(xiàn)中文亂碼,對于varchar類型把它設(shè)為binary屬性就可以解決中文問題,對于text類型就要用一個(gè)編碼轉(zhuǎn)換類來處理,實(shí)現(xiàn)如下:
public class Convert {
/** 把ISO-8859-1碼轉(zhuǎn)換成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("編碼轉(zhuǎn)換錯(cuò)誤:"+e.getMessage());
return "";
}
}
}
把它編譯成class,就可以調(diào)用Convert類的靜態(tài)方法ISOtoGB()來轉(zhuǎn)換編碼。
posted on 2006-11-12 20:08 小雨不打傘 閱讀(3810) 評論(0) 編輯 收藏 所屬分類: web學(xué)習(xí)心得