java亂碼問題

1、在獲取session數據時出現亂碼:

解決方法:String   str1   =   new   String(str.getBytes( "iso-8859-1 "), "GB2312 ");

2、html顯示出現亂碼:

解決方法:

<head>

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

3、客戶端提交中文,Servlet獲取是亂碼:

如果是get方式,在tomcat中,在conf/server.xml文件中8080端口的connector元素增加URIEncoding="utf-8" 如果是post方式,在servlet中,request.setcharactorEncoding("utf-8");放在request.getparameter之前。

4、Servlet提交中文,客戶端獲取亂碼
Servlet中加入response.setCharactorEncoding("utf-8");該代碼要放在response.getWriter()之前。

5、javascript中提交數據在服務器端獲取出現亂碼:

解決方法:

客戶端代碼設置:<script type="text/javascript" charset="gb2312">要爲“gb2312”,用其他的試過都不行。

服務端代碼獲取:new String(request.getParameter("name").getBytes("ISO-8859-1"),"gb2312");

6、jsp頁面中ajax提交數據後臺獲取出現亂碼

第一種情況:$.get("/AjaxServer?name="+$("#username").val(),null,function(data){});

客戶端使用編碼:gb2312,

後臺獲取採用:gb2312

第二種情況:$.ajax({
type:"Get",
url:"/AjaxServer", //servlet
dataType:"text",
data:{name:username},
success:function(data){
alert(username);
alert(data);
}
})

客戶端採用編碼:new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8");


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章