ts7758258 发表于 2015-8-7 08:17:53

关于上个Flex-Jsp-DB例子中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)

情况:

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页面用页面指令设置,返回结果是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



















test.mxml

其中HTTPService使用自定义request对象传递数据,注意前面的System.useCodepage = true;语句









   

   



   

      {phoneService.result.phonelist.phone}

   





   

         

   



   

         

            

               

                     

                     

                     

                     

               

            

         

         

         

         

   

   

   

         

            

               

               

               

            

         

   



   

页: [1]
查看完整版本: 关于上个Flex-Jsp-DB例子中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)