JSP中的中文字元處理

2021-04-18 03:59:32 字數 743 閱讀 7673

jsp中,在接受request的中文字元時,顯示出來的是亂碼,jsp中含客戶提交的含有漢字的資料,要採用特殊的處理方式,先將得到的字串用iso-8895-1編碼,並放到乙個位元組陣列中,再用string類的建構函式將其轉換為字串物件,

string tempstr=request.getparameter("str"); byte tb=tempstr.getbytes("iso-8859-1"); tempstr=new string(tb);

另外,為了方便使用,我們可以將其放在乙個類裡,或直接定義在jsp頁面裡,例1:

public string codetostring(string str) catch(exception e) }

例2:

public static string gbktoiso(string str) catch (unsupportedencodingexception e) catch (exception e) }

另外還有情況是jsp之間的引數傳遞,當引數為中文時,會出現接收到的相應引數為空的情況如:../test.jsp?name="引數1"&deals="test" 可以發現,name沒有傳過去,而deals可以正常傳過去,解決這種情況的方法為先將中文轉碼: name=urlencoder.encode(name,"utf-8"); 在接收的jsp中,將轉碼的中文解碼: string name=urldecoder.decode(request.getparameter("name"),"utf-8");

JSP中的中文字元處理

在編寫jsp程式時,常常會碰到中文字元處理的問題,在接受request的中文字元時顯示出來一串亂碼。網上處理方法一籮筐,下面說說我用過的兩種有效地解決辦法 1.為程式編寫乙個字串處理函式,用乙個靜態檔案儲存,在需要處理中文字元的jsp頁面中包含它,public string codetostring...

JSP中的中文字元處理 轉貼

jsp中,在接受request的中文字元時,顯示出來的是亂碼,jsp中含客戶提交的含有漢字的資料,要採用特殊的處理方式,先將得到的字串用 iso 8859 1 編碼,並放到乙個位元組陣列中,再用string類的建構函式將其轉換為字串物件 string tempstr request.getparam...

python json中文字元處理

如果json中包含中文字元,我們將其寫入檔案中會怎樣呢?import json data with open data.json w as file file write json.dumps data,indent 2 引數indent指定縮排數。執行之後,結果如下 可以看到結果中,中文字元都變成...