當調用request.getparameter()函數時,會自動進行一次uri的解碼過程,調用時內建的解碼過程會導致亂碼出現。而uri 編碼兩次後,request.getparameter()函數得到的是原資訊uri編碼一次的內容。再用可控的解碼函數 java.net.urldecoder.decode()就可解出原始的正確的資訊
response.setheader("charset","gb2312");
看到的說明原文如下:
用ajax中採用get來讀取頁面時,responsetext裡面的中文多半會出現亂碼,這是因為xmlhttp在處理返回的responsetext的時候,resposebody預設採用utf-8編碼,如果伺服器送出的確實是utf-8的資料流的時候漢字會正確顯示,而讀取的頁面編碼是gbk或者gb2312時就會出現頁面中文的亂碼。
解決的辦法有二:
1、在送出的流裡面加一個header,指明送出的是具體編碼流:
php教程:header('content-type:text/html;charset=gb2312');
asp教程:response.charset("gb2312")
jsp教程:response.setheader("charset","gb2312");
2、採用utf-8編碼來儲存html文檔
看個ajax jsp亂碼解決方案
ajaxpost.js
var myrequest; // variable to hold request object
function mysubmit(){
if (window.xmlhttprequest)
{
myrequest = new xmlhttprequest(); // standards-compliant browsers
} else if (window.activexobject)
{
myrequest = new activexobject("msxml2.xmlhttp"); // for ie
}
var post="name="+document.getelementbyid("postval").value;
post=encodeuri(post);
post=encodeuri(post); //最重要的部分,兩次調用encodeuri ,就是編碼兩次
myrequest.open("post","servlet/display",false);
//myrequest.setrequestheader("contentlength",post.length);
myrequest.setrequestheader("content-type","application/x-www-form-urlencoded");
myrequest.send(post);
var res=myrequest.responsetext;//接收返回的資料
document.getelementbyid("display").innerhtml=res;
}
ajaxpost.jsp
<%@ page language="java" import="java.util.*" pageencoding="gb2312"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>my jsp 'ajaxpost.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<script type="text/網頁特效" src="js/ajaxpost.js"></script>
</head>
<body>
<textarea rows="10" cols="20" id=postval></textarea>
<br>
<input type=button onclick="mysubmit();" value="列印">
<div id="display"></div>
</body>
</html>
servlet
public void dopost(https教程ervletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html");
response.setcharacterencoding("gbk");
printwriter out = response.getwriter();
string name=request.getparameter("name");
name=urldecoder.decode(name,"utf8"); //post 傳遞的時候,一定是用utf8編碼的,url 自己可以設定
system.out.println(name);
out.println(name);
out.flush();
out.close();
}
執行個體二
初始頁面內容如下(hello.jsp):
<%@ page language="java" import="java.util.*" pageencoding="gb18030"%>
<%string path = request.getcontextpath();%>
<!doctype html public "-//www.111cn.net//dtd html 4.01 transitional//en">
<html>
<head>
<title>ajax提交頁面</title>
<meta http-equiv="content-type" content="text/html; charset=gb18030">
<script type="text/javascript">
function justdo(){
var post="name=王力猛&email=wallimn@sohu.com&bokee=http://down.111cn.net";
post = encodeuri(post);
post = encodeuri(post);//兩次,很關鍵
var xmlobj = new activexobject('msxml2.xmlhttp');
var url = '<%= path%>/page/act.jsp';//檔案名稱需要調整成測試時的相應位置?
xmlobj.open ('post',url,true);
xmlobj.setrequestheader("cache-control","no-cache");
xmlobj.setrequestheader("content-type","application/x-www-form-urlencoded");
xmlobj.send (post);//注意:post方式,使用這個來發送內容?
}
</script>
</head>
<body>
<input type="button" value="提交" onclick="justdo()"/>
</body>
</html>
/////////////////////////////////////////////////////////////////////////////////////
ajax請求處理頁面(act.jsp)的內容如下:
/////////////////////////////////////////////////////////////////////////////////////
<%@ page language="java" import="java.util.*" pageencoding="gb18030"%>
<%string path = request.getcontextpath();%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<%@page import="java.net.urldecoder"%>
<html>
<head>
<title>ajax deal</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<%
//遍曆輸出參數內容。
for (enumeration e = request.getparameternames(); e.hasmoreelements();) {
string h = (string) e.nextelement();
string v = request.getparameter(h);
string mm = java.net.urldecoder.decode(v, "utf-8");
system.out.println("請求參數: " + h + " = " + mm);
}
%>
</body>
</html>
/////////////////////////////////////////////////////////////////////////////////////