<%@ page contenttype="text/html;charset=gb2312%>
如果是用servlet就加
代碼如下:
response.setcontenttype("text/html;charset=gb2312");
request.setcharacterencoding("gb2312");
還有一個更好的方法就是在加一個filter
在其中加入
代碼如下:
response.setcontenttype("text/html;charset=gb2312");
request.setcharacterencoding("gb2312");
一切都解決了
再說一下從客戶端上傳數據,就必須在服務端進行編碼轉換
代碼如下:
string param = request.getparamter("param");
param = new string(param.getbytes("iso-8859-1"),"gb2312");
現在就都是中文的了。
注意前后臺編碼一致.你用的是中文.而ajax傳輸數據的時候用的是utf-8
代碼如下:
<script>
var oxmlhttp = new activexobject( "microsoft.xmlhttp ");
oxmlhttp.open( "get ", "http://dotnet.aspx.cc/content.aspx ", false);
oxmlhttp.send()
var ostream = new activexobject( "adodb.stream ");
if(ostream == null)
alert( "您的機器不支持adodb.stream. ")
else
{
ostream.type=1;
ostream.mode=3;
ostream.open() ;
ostream.write(oxmlhttp.responsebody);
ostream.position= 0;
ostream.type= 2;
ostream.charset= "gb2312 ";
var result= ostream.readtext();
ostream.close();
ostream = null;
alert( result);
}
</script>
客戶端文件的編碼設置為gb2312,如下面代碼所示:
html代碼
代碼如下:
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
在發送的url地址中的查詢字符串或者是使用post方式發送的請求內容不要使用escape函數進行編碼,切記!
在服務器端的jsp教程文件也設置為gb2312編碼格式,如下面代碼所示:
jsp 代碼
或者設置response的頭,如下面代碼所示:
代碼如下:
response.setheader("content-type","text/html; charset=gb2312");
兩者原理是一樣的。
最著關鍵的是在獲取參數時應該對獲取字符串進行重新編碼,如下面代碼所示:
代碼如下:
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"gb2312");
其中,username為接收的參數。
直接使用out.print(username);就可以將中文返回給客戶端,在客戶端直接使用xmlhttp.responsetext屬性就可以直接使用返回的中文了!
附件中我測試用的一個小例子,在tomcat6.0和resin2.1.8中通過測試!其實,還有一個一勞永逸的解決方案,就是添加一個過濾器。
補充一下提交方法為get時時在服務器里寫的時這句代碼
代碼如下:
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"gb2312");
為post時應該時這樣吧
代碼如下:
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"utf-8");
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com