情況:
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使用中文內容
create table tblMobile (id int, name varchar(20), price decimal(10,2), image varchar(50), categoryid varchar(20))

phonelist.jsp
這里數據庫連接是SqlServer2000
<?xml version="1.0" encoding="utf-8"?>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<phonelist>
<%
    
String sql = "";
    
String url = "";

    
String categoryID = request.getParameter("categoryID");

    
try {
        
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
        url 
= "jdbc:microsoft:sqlserver://myserver:1433;DatabaseName=flex;User=flex;Password=flex;";
        Connection conn 
= DriverManager.getConnection(url);
        Statement stmt 
= conn.createStatement();

        
        
String strOut = new String(categoryID.getBytes("ISO8859-1"), "GBK"); 

        System.out.println(
"categoryID="+categoryID);
        System.out.println(
"categoryID="+strOut);

        sql 
= "select id, name, price, image from tblMobile where categoryid='" + strOut + "'";
        ResultSet rs 
= stmt.executeQuery(sql);

        
while (rs.next()){
            out.println(
"<phone id=\"" + rs.getString(1) + "\">");
            out.println(
"<id>" + rs.getString(1+ "</id>");
            out.println(
"<name>" + rs.getString(2+ "</name>");
            out.println(
"<price>" + rs.getString(3+ "</price>");
            out.println(
"<image>" + rs.getString(4+ "</image>");
            out.println(
"</phone>");
        }

        rs.close();
        stmt.close();
        conn.close();

    } 
catch (Exception e) {
        out.println(e);
    }
%
>
</phonelist>

test.mxml
其中HTTPService使用自定義request對象傳遞數據,注意前面的System.useCodepage = true;語句
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
    verticalGap
="10"
    backgroundColor
="#FFFFFF"
    pageTitle
="手機"
    initialize
="initApp()">
    
    
<mx:HTTPService id="phoneService" url="phonelist.jsp" fault="alert(event.fault.faultstring)"/>

    
<mx:Model id="phonelist">
        {phoneService.result.phonelist.phone}
    
</mx:Model>


    
<mx:Script>
        
<![CDATA[

        var categoryId = 1;
        var categoryName = "Moto";
    
        function initApp() {
            System.useCodepage = true;
            categoryId = "目錄1";
            var obj = new Object();
            obj["categoryID"] = categoryId;
            phoneService.send(obj);
        }

        
]]>
    
</mx:Script>

    
<mx:HBox>
        
<mx:LinkBar styleName="title" width="500" click="" >
            
<mx:dataProvider>
                
<mx:Array>
                    
<mx:Object label="首 頁" link="main"/>
                    
<mx:Object label="手機分類" link="catagory"/>
                    
<mx:Object label="論 壇" link="forum"/>
                    
<mx:Object label="關 于" link="about"/>
                
</mx:Array>
            
</mx:dataProvider>
        
</mx:LinkBar>
        
<mx:Label text="搜索"/>
        
<mx:TextInput id="key" width="120"/>
        
<mx:Button label="Go" click="initApp();"/>
    
</mx:HBox>
    
    
<mx:DataGrid dataProvider="{phonelist}">
        
<mx:columns>
            
<mx:Array>
                
<mx:DataGridColumn columnName="id" headerText="ID"/>
                
<mx:DataGridColumn columnName="name"  headerText="Name"/>
                
<mx:DataGridColumn columnName="image"  headerText="Image"/>
            
</mx:Array>
        
</mx:columns>
    
</mx:DataGrid>

    
<mx:HBox horizontalAlign="center">
        
<mx:Label text="Copy Right 2004  dannyr's Studio "/>
    
</mx:HBox>
</mx:Application>

結果:
在Jsp頁面里按8859_1編碼可以成功獲取Flex傳遞的中文內容。

備注:
這個方法是對Tomcat的,其他的Java應用服務器的Request處理方式可能不同,應區分對待!

引用:
以下是Flex文檔關于System.useCodepage的說明:(比較簡單,就不翻譯了)

System.useCodepage

Availability

flash Player 6.

Usage

System.useCodepage:Boolean

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.

  • When the property is set to false, flash Player interprets external text files as Unicode. (These files must be encoded as Unicode when you save them.)
  • When the property is set to true, flash Player interprets external text files using the traditional code page of the operating system running the player.