情況:
Flex默認(rèn)使用的都是utf-8編碼,包括Get,Post等方法。而Tomcat服務(wù)器端接收request對象默認(rèn)是8859_1編碼,添加Tomcat的request Filter用request.setCharacterEncoding("utf-8"); 來設(shè)置,這個(gè)方法屬于Tomcat設(shè)置和Flex無關(guān),暫不討論!

flex->Jsp:
有2種情況
情況一、MXML源代碼文件中寫入的中文字符:
Flex使用 System.useCodepage = true;即使用本地操作系統(tǒng)編碼(GBK) 設(shè)置Flex的處理編碼。Jsp中用依然用ISO_8859_1編碼來處理,并轉(zhuǎn)化為GBK。這樣Jsp可以正確解釋Flex傳遞的中文字符。 這個(gè)時(shí)候可以認(rèn)為Flex對mxml源代碼文件進(jìn)行編譯時(shí)候,源代碼中的中文字符已經(jīng)混亂了,所以要加上System.useCodepage = true;語句,按GBK編碼將中文字符從Flex發(fā)送到Tomcat。
同時(shí)Tomcat中Jsp應(yīng)該按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運(yùn)行時(shí)候由輸入框輸入的中文字符
這個(gè)時(shí)候輸入框輸入的中文字符是一定為UTF-8編碼的,所以Flex中System.useCodepage = false;或者不設(shè)置,就默認(rèn)utf-8編碼格式傳遞數(shù)據(jù),而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"%>設(shè)置,返回結(jié)果是utf-8編碼,F(xiàn)lex接收后成功解釋并正確顯示。

測試環(huán)境:
Windows2000 Server    (字符集為GBK)
Tomcat 5.0.28    (默認(rèn)設(shè)置)
JDK1.5.0
flex 1.5    (默認(rèn)設(shè)置)
SqlServer2000 Sp3


測試代碼:僅僅為第二種情況,第一種情況酌情修改即可
表結(jié)構(gòu)
其中categoryid使用中文內(nèi)容
create table tblMobile (id int, name varchar(20), price decimal(10,2), image varchar(50), categoryid varchar(20))

phonelist.jsp
這里數(shù)據(jù)庫連接是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對象傳遞數(shù)據(jù),注意前面的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
="手機(jī)"
    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="手機(jī)分類" link="catagory"/>
                    
<mx:Object label="論 壇" link="forum"/>
                    
<mx:Object label="關(guān) 于" 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>

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

備注:
這個(gè)方法是對Tomcat的,其他的Java應(yīng)用服務(wù)器的Request處理方式可能不同,應(yīng)區(qū)分對待!

引用:
以下是Flex文檔關(guān)于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.