天天看点

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()?芥?般??