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.
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
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<phonelist>
<%
String sql = "";
String url = "";
String categoryID = request.getParameter("categoryID");
try {
Class .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;语句
<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
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.