甘先生Blog生活就像一盒巧克力,你永遠不知道你會得到什么 |
常用鏈接留言簿(16)我參與的團隊隨筆分類(66)
隨筆檔案(59)文章分類(45)
文章檔案(29)相冊JavaLinuxOpen SourceOracleOtherUnixWebDesign技術Blog朋友Blog高手Blog搜索最新評論
閱讀排行榜評論排行榜 |
|
Copyright @
甘先生
Powered by:
.Text and
ASP.NET
Theme by: .NET Monster
Flex默認使用的都是utf-8編碼,包括Get,Post等方法。而Tomcat服務器端接收request對象默認是8859_1編碼,添加Tomcat的request Filter用request.setCharacterEncoding("utf-8"); 來設置,這個方法屬于Tomcat設置和Flex無關,暫不討論!
flex->Jsp:
有2種情況
情況一、MXML源代碼文件中寫入的中文字符:
Flex使用 System.useCodepage = true;即使用本地操作系統編碼(GBK) 設置Flex的處理編碼。Jsp中用依然用ISO_8859_1編碼來處理,并轉化為GBK。這樣Jsp可以正確解釋Flex傳遞的中文字符。 這個時候可以認為Flex對mxml源代碼文件進行編譯時候,源代碼中的中文字符已經混亂了,所以要加上System.useCodepage = true;語句,按GBK編碼將中文字符從Flex發送到Tomcat。
同時Tomcat中Jsp應該按GBK重新編碼
String categoryID = request.getParameter("categoryID");
String strOut = new String(categoryID.getBytes("ISO8859-1"), "GBK");
System.out.println("categoryID="+categoryID);
System.out.println("categoryID="+strOut);
情況二、Flex運行時候由輸入框輸入的中文字符
這個時候輸入框輸入的中文字符是一定為UTF-8編碼的,所以Flex中System.useCodepage = false;或者不設置,就默認utf-8編碼格式傳遞數據,而Tomcat中Jsp使用下面語句按UTF-8來重新編碼
String categoryID = request.getParameter("categoryID");
String strOut = new String(categoryID.getBytes("ISO8859-1"), "utf-8");
System.out.println("categoryID="+categoryID);
System.out.println("categoryID="+strOut);
Jsp->Flex:
Jsp頁面用頁面指令<%@ page contentType="text/html;charset=utf-8"%>設置,返回結果是utf-8編碼,Flex接收后成功解釋并正確顯示。
測試環境:
Windows2000 Server (字符集為GBK)
Tomcat 5.0.28 (默認設置)
JDK1.5.0
flex 1.5 (默認設置)
SqlServer2000 Sp3
測試代碼: (僅僅為第二種情況,第一種情況酌情修改即可)
表結構
其中categoryid使用中文內容
phonelist.jsp
這里數據庫連接是SqlServer2000
test.mxml
其中HTTPService使用自定義request對象傳遞數據,注意前面的System.useCodepage = true;語句
結果:
在Jsp頁面里按8859_1編碼可以成功獲取Flex傳遞的中文內容。
備注:
這個方法是對Tomcat的,其他的Java應用服務器的Request處理方式可能不同,應區分對待!
引用:
以下是Flex文檔關于System.useCodepage的說明:(比較簡單,就不翻譯了)
System.useCodepage
Availability
flash Player 6.
Usage
Description
Property; a Boolean value that tells flash Player whether to use Unicode or the traditional code page of the operating system running the player to interpret external text files. The default value of System.useCodepage is
false
.false
, flash Player interprets external text files as Unicode. (These files must be encoded as Unicode when you save them.)true
, flash Player interprets external text files using the traditional code page of the operating system running the player.