天天看點

JSP亂碼問題的解決

涓???JSP椤甸?㈡?劇ず涔辯???

浜???琛ㄥ????浜や腑???跺?虹?頒貢???

涓????版??搴?杩??ユ?跺?虹?頒貢???

澶у?跺??SP??寮???杩?绋?涓?锛?缁?甯稿?虹?頒腑??涔辯??????棰?锛????戒??沖?版?扮????????闆?ㄦ??????SP寮???涓????扮??涓???涔辯??????棰???瑙e?沖??娉????烘?ヤ?澶у?跺???????

?

涓???JSP椤甸?㈡?劇ず涔辯???

涓??㈢???劇ず椤甸??display.jsp)灏卞?虹?頒貢??锛?

?

<html>?

<head>?

<title>JSP??涓???澶???</title>?

<meta?http-equiv="Content-Type"?content="text/html;?charset=gb2312">?

</head>?

<body>?

<%?

out.print("JSP??涓???澶???");?

%>?

</body>?

????????????</html>

瀵逛?????WEB???″?ㄥ??涓?????JDK????锛?澶???缁???灏變?涓??楓??????锛????″?ㄤ嬌?ㄧ??缂????瑰?涓?????娴?瑙??ㄥ?逛?????瀛?绗??劇ず缁???涓?????瀵艱?寸????瑙e?沖??娉?锛???SP椤甸??腑??瀹?缂????瑰?(gb2312),?沖?ㄩ〉?㈢??绗?涓?琛???涓?锛??

?

?辨??浠g??<%@?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>

?

浜???琛ㄥ????浜や腑???跺?虹?頒貢???

涓??㈡??涓?涓???浜ら〉??submit.jsp)锛?浠g??濡?涓?锛??

?

<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)浠g??锛??

?

<%@?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??浜よ?辨??瀛?绗??芥?g‘?劇ず锛?濡?????浜や腑???跺氨浼??虹?頒貢????????锛?娴?瑙??ㄩ?璁や嬌??TF-8缂????瑰??ュ????璇鋒?锛???UTF-8??GB2312缂????瑰?琛ㄧず瀛?绗??朵?涓??鳳?杩??峰氨?虹?頒?涓??借????瀛?绗???瑙e?沖??娉?:??杩?request.seCharacterEncoding("gb2312")瀵矽?鋒?杩?琛?缁?涓?缂???锛?灏卞???頒?涓?????姝e父?劇ず??淇??瑰????process.jsp浠g??濡?涓?锛??

?

<%@?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>

?

涓????版??搴?杩??ュ?虹?頒貢???

??瑕?娑???涓??????版?瑰?ㄩ?ㄦ??涔辯??锛?瑙e?沖??娉?锛??ㄦ?版??搴????版??搴?URL涓???涓?useUnicode=true&characterEncoding=GBK灏?K浜????

?

?????版??搴????劇ず涔辯???

??ysql4.1.0涓?,varchar绫誨??锛?text绫誨??灏變??虹?頒腑??涔辯??锛?瀵逛?varchar绫誨????瀹?璁句負binary灞??у氨??浠ヨВ?充腑????棰?锛?瀵逛?text绫誨??灏辮??ㄤ?涓?缂???杞??㈢被?ュ???锛?瀹??闆?涓?锛??

?

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";?

????????????}

?

瀛?绗????ユ?版??搴??剁???gb2iso()?芥?幫?灏?瀛?绗???版??搴????烘?訛??????iso2gb()?芥?般??