加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

引用 Flex与Servlet交互数据心得

发布时间:2020-12-15 01:35:05 所属栏目:百科 来源:网络整理
导读:flex与servlet交互数据,有以下几个方面需要注意 参数的传递: 中文乱码的解决 返回值的处理 以下是我今天写的一个执行正常的程序 flex代码如下: ??xml version="1.0" encoding="utf-8"? mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml " layou

flex与servlet交互数据,有以下几个方面需要注意

参数的传递:

中文乱码的解决

返回值的处理

以下是我今天写的一个执行正常的程序

flex代码如下:

?<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
?<mx:Script>
??<![CDATA[
???import mx.rpc.events.ResultEvent;
???import mx.controls.Alert;
???
???private function doRequest():void{
???this.btn_do.enabled=false;
???var url:String="http://localhost:8080/Test/servlet/BUAA";
???this.sevlet.url=url;
???var param:URLVariables=new URLVariables();
???//这个对象是用来设置url中需要传递的参数,将所传递的参数一一按照如下的格式设置,即可传递到后台服务器
???
???//如下所示传递了两个参数,一个是username,一个是timestamp
???param.username=this.txt.text;
???param.timestamp=(new Date()).toString();
???//////////////////////////////////////////////////////////////////////////////////
???
???this.sevlet.send(param);//
???}
???
????? //下面这个函数是服务器处理完毕后flex的处理函数,在下文的标签里有定义
???private function resultHandler(event:ResultEvent):void{
????Alert.show(event.result.user);
????this.btn_do.enabled=true;
???}
??]]>
?</mx:Script>
?<mx:HTTPService id="sevlet" resultFormat="e4x" result="resultHandler(event)"/>
?<mx:Panel title="测试与servlet后台交互" layout="absolute" width="100%" height="90%">
?<mx:VBox>
?
??????? <mx:Button id="btn_do" label="取得数据" click="doRequest();" fontSize="14"/>
??????? <mx:TextInput id="txt" text="输入文字" fontSize="14"/>???
??????? ??
?</mx:VBox>????
??? </mx:Panel>
</mx:Application>

Servlet端的代码如下

?public void doGet(HttpServletRequest request,HttpServletResponse response)
???throws ServletException,IOException {
??response.setContentType("text/xml;charset=utf-8");
??String para = request.getParameter("username");
??PrintWriter out = response.getWriter();
??String strOut = new String(para.getBytes("ISO8859-1"),"utf-8");
??out.println("<zpa>");
??out.println("<user>"+strOut+"</user>");
??out.println("</zpa>");
??out.flush();
??out.close();
?}

?

对于中文参数的传递,在flex里有以下两个对应的处理函数

????
???//下面的这2个函数是用来处理flex中传递中文参数乱码问题的,一个编码,一个解码
???private function htmlParaEncoding(para:String):String{
????return encodeURIComponent(para);
???}
???private function htmlParaDecoding(para:String):String{
????return decodeURIComponent(para);
???}

不过一般在flex端只需要用编码函数,java中打印结果时使用java.net.URLDecoder.decode方法解码,如下面这个例子

flex代码:

?<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
??? layout="absolute" fontSize="12" xmlns:local="*">
??? <mx:TraceTarget/>
??? <mx:Style>
??????? .lab{
?????????? fontWeight: "bold";
?????????? /*color: #FF0000;*/
?????????? fontSize: 15;
??????? }
??? </mx:Style>
??? <mx:Script>
??????? <![CDATA[
??????? //对提交给后台的参数进行UTF-8的编码处理
??????? private function httpEncoding(param:String):String{
??????????? return encodeURIComponent(param);
??????? }
??????? private function doLogin():void {
??????????? //trace("focusEnabled:"+loading.focusEnabled);
??????????? //this.focusManager.setFocus(user);
??????????? var url:String = "http://localhost:8080/Test/servlet/BUAA";
??????????? var params:URLVariables = new URLVariables();
??????????? //这个user,psw就是传入后台的参数user,jsp就用 request.getParameter("user")来取
??????????? params.user = httpEncoding(user.text);
??????????? params.psw = psw.text;
??????????? var loader:URLLoader = new URLLoader();
??????????? this.configureEventListeners(loader);
??????????? //可以不设置,因为默认是text
??????????? loader.dataFormat = URLLoaderDataFormat.TEXT;
??????????? var request:URLRequest = new URLRequest(url);
??????????? request.data = params;
??????????? try{
??????????????? loader.load(request);
??????????? }catch(error:Error){
??????????????? trace(error.message);
??????????? }
??????? }
??????? private function configureEventListeners(dispatcher:IEventDispatcher):void {
??????????? dispatcher.addEventListener(Event.COMPLETE,completeHandler);
??????????? dispatcher.addEventListener(Event.OPEN,openHandler);
??????????? dispatcher.addEventListener(ProgressEvent.PROGRESS,progressHandler);
??????????? dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
??????????? dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler);
??????????? dispatcher.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
??????? }
?????? private function completeHandler(event:Event):void {
??????????? var loader:URLLoader = URLLoader(event.target);
??????????? trace("--complete..."+event.target.data);
??????????? //var dataXML:XML = XML(event.target.data);
??????????? //trace(dataXML.toXMLString());
??????????? btn_btn.enabled=true;
??????? }

??????? private function openHandler(event:Event):void {
??????????? trace("openHandler: " + event);
??????????? //this.focusManager.setFocus(loading);
??????????? btn_btn.enabled=false;
??????? }

??????? private function progressHandler(event:ProgressEvent):void {
??????????? trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
??????? }

??????? private function securityErrorHandler(event:SecurityErrorEvent):void {
??????????? trace("securityErrorHandler: " + event);
??????? }

??????? private function httpStatusHandler(event:HTTPStatusEvent):void {
??????????? trace("httpStatusHandler: " + event);
??????? }

??????? private function ioErrorHandler(event:IOErrorEvent):void {
??????????? trace("ioErrorHandler: " + event);
??????? }
??????? ]]>
??? </mx:Script>
???
??? <mx:Panel title="欢迎登录WAP管理系统:" width="307" height="189" layout="absolute" verticalAlign="top" horizontalCenter="-10.5" verticalCenter="-9">
??????? <mx:Label x="32" y="25" text="登录口令:" width="59"/>
??????? <mx:TextInput id="user" x="99" y="23" width="147"/>
??????? <mx:Label x="32" y="53" text="登录密码:" width="59"/>
??????? <mx:TextInput id="psw" x="99" y="51" displayAsPassword="true" width="147"/>
??????? <mx:ControlBar alpha="1">
??????????? <mx:Button id="btn_btn" x="58" y="92" label="确 定" click="this.doLogin();"/>
??????????? <mx:Button x="162" y="92" label="取 消"/>
??????????? <mx:Label x="0" y="129" text="Powered by Keren" textAlign="right"/>
??????? </mx:ControlBar>
??? </mx:Panel>
</mx:Application>

java代码

? String usr = java.net.URLDecoder.decode(request.getParameter("user"),"UTF-8");
? System.out.println("取得传入的参数:"+usr);
? String psw = java.net.URLDecoder.decode(request.getParameter("psw"),"UTF-8");
? System.out.println("取得传入的参数:"+psw);
??out.flush();
??out.close();

?

此外,在网上也参考到一些不错的文章,摘录如下:

?

http://www.cnblogs.com/dannyr/archive/2004/11/24/68026.html

原文

情况:

Flex默认使用的都是utf-8编码,包括Get,Post等方法。而Tomcat服务器端接收request对象默认是8859_1 编码,添加Tomcat的request Filter用request.setCharacterEncoding("utf-8"); 来设置,这个方法属于Tomcat设置和Flex无关,暂不讨论!

flex?->Jsp:

有2种情况 //我试验第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接收后成功解释并正确显示。

?

原文:http://haitao-love-u.blog.sohu.com/116743431.html

通常办法在jsp页面中,加入<% request.setCharacterEncoding("utf-8"); %>,flex默认编码是utf-8。然而,如果问题没有解决,依旧是乱码的话,那么,打开你的tomcat目录下的conf/server.xml文件,观察:?<Connector port="8080" protocol="HTTP/1.1"

?????????????? maxThreads="150" connectionTimeout="20000"

?????????????? redirectPort="8443" URIEncoding="UTF-8"/>,若没有URIEncoding="UTF-8",添加进去,这样问题就圆满解决了。顺便说下,mysql数据库编码也是utf-8

?原文:http://java-007.javaeye.com/blog/381617

最近看一些文档,总结了一些<mx:HTTPService>给后台传递参数的方法,列举如下: 方法1:采用URLVariables对象 <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" ???? layout="absolute" fontSize="12" ??? > ??? <mx:Script> ??????? <![CDATA[ ??????????? import mx.controls.Alert; ??????????? import mx.rpc.events.ResultEvent; ??????????? //对提交给后台的参数进行UTF-8的编码处理 ??????????? private function httpEncoding(param:String):String{ ??????????????? return encodeURIComponent(param); ??????????? } ??????????? private function httpEncoding0(param:String):String{ ??????????????? return param;//encodeURI(param); ??????????? } ??????????? private function doRequest():void{ ??????????????? btn_do.enabled=false; ??????????????? var url:String = "http://localhost:8600/grid.jsp"; ??????????????? //以下那样写后台会乱码,不管是否做URI编码转换 ??????????????? //url += "?user="+httpEncoding0("用户名"); ??????????????? //url += "&psw="+httpEncoding0("密码"); ??????????????? //trace(url); ??????????????? srv.url = url; ??????????????? //srv.send(); ??????????????? //以下这样写正常 ??????????????? var params:URLVariables = new URLVariables(); ??????????????? //这个user,jsp就用 request.getParameter("user")来取 ??????????????? params.user = httpEncoding("用户名"); ??????????????? params.psw = httpEncoding("密码"); ??????????????? srv.send(params);??????????? ??????????? } ??????????? private function resultHandler(event:ResultEvent):void{ ??????????????? Alert.show("与后台交互结束,前台开始取得的数据...","提示信息"); ??????????????? btn_do.enabled=true; ??????????? } ??????? ]]> ??? </mx:Script> ??? <mx:HTTPService id="srv" result="resultHandler(event);"/> ??? <mx:Panel title="测试与jsp后台交互" layout="absolute" width="100%" height="90%"> ??????? <mx:Button id="btn_do" label="取得数据" click="doRequest();"/> ??????? <mx:Spacer height="1"/> ??????? <mx:DataGrid dataProvider="{srv.lastResult.catalog.product}" width="100%" height="100%" y="28"/>???? ??? </mx:Panel> </mx:Application> 方法2:采用<mx:request/>,同时也演示了mx:State的用法,[来自网上] <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> ??? <mx:states> ??????? <mx:State name="Logged In"> ??????????? <mx:SetProperty target="{panel1}" name="width" value="95%"/> ??????????? <mx:SetProperty target="{panel1}" name="height" value="95%"/> ??????????? <mx:RemoveChild target="{password}"/> ??????????? <mx:RemoveChild target="{username}"/> ??????????? <mx:RemoveChild target="{label1}"/> ??????????? <mx:RemoveChild target="{Submit}"/> ??????????? <mx:RemoveChild target="{label2}"/> ??????????? <mx:SetProperty target="{panel1}" name="title" value="Members Section"/> ??????????? <mx:AddChild relativeTo="{panel1}" position="lastChild"> ??????????????? <mx:Label x="10" y="10" text="Welcome to the Members Section!"/> ??????????? </mx:AddChild> ??????????? <mx:AddChild relativeTo="{panel1}" position="lastChild"> ??????????????? <mx:Label x="10" y="36" text="Here you can do great things,like join the forums @ Viper Creations!"/> ??????????? </mx:AddChild> ??????????? <mx:AddChild relativeTo="{panel1}" position="lastChild"> ??????????????? <mx:Label x="10" y="62" text="Label"/> ??????????? </mx:AddChild> ??????? </mx:State> ??? </mx:states> ??? <mx:Script> ??????? <![CDATA[ ??????????? import mx.rpc.events.ResultEvent; ??????????? ??????? ]]> ??? </mx:Script> ??? <mx:Script> ??? <![CDATA[ private function checkLogin(evt:ResultEvent):void { ??? if(evt.result.loginsuccess == "yes") ??? { ??? currentState = "Logged In"; ??? } ??? if(evt.result.loginsuccess == "no") ??? { ??????? ??????? mx.controls.Alert.show(''Invalid username/password''); ??? }??????? } ]]> </mx:Script> ??? <mx:HTTPService id="login_user" result="checkLogin(event)" showBusyCursor="true" method="POST" url="http://www.vipercreations.com/site_admin/login.php" useProxy="false"> ??????? <mx:request xmlns=""> ??????????? <username> ??????????????? {username.text} ??????????? </username> ??????????? <password> ??????????????? {password.text} ??????????? </password> ??????? </mx:request> ??? </mx:HTTPService> ??? ??? <mx:Panel resizeEffect="Resize" width="250" height="200" layout="absolute" title="Login System" horizontalCenter="0" verticalCenter="-2" id="panel1"> ??????? <mx:Label x="10" y="10" text="Username:" id="label1"/> ??????? <mx:TextInput x="10" y="36" id="username"/> ??????? <mx:Label x="10" y="66" text="Password:" id="label2"/> ??????? <mx:TextInput x="10" y="92" id="password" displayAsPassword="true"/> ??????? <mx:Button x="10" y="122" label="Submit" id="Submit" click="login_user.send();"/> ??? </mx:Panel> ??? </mx:Application>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读