这一次,改用flex来做一个远程与jsp访问,并返回xml数据值 下面是一个封装好的类,继承了HttpService类
package Config { import flash.events.*; import flash.net.*; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; public class Connect2 extends HTTPService { private static var connect:Connect2=null; //连接的网址 public static var myurl:String="http://localhost:8080/flash/receive.jsp"; public function Connect2() { this.method="POST"; this.useProxy=false; //不启动代理服务 this.resultFormat="xml";//设置返回数据的格式为xml } public static function getconnect():Connect2 { if (connect == null) { connect=new Connect2(); } return connect; } public function sendMessage(para:URLVariables,myurl:String):void { //设置发送数据的变量 this.url=myurl;//指定发送的jsp页面 this.send(para);//发送数据 } } }
第二:jsp输出值
out.println(“你要返回的值”);通过flex来读取
<% response.setContentType("text/xml"); String myname=request.getParameter("userName"); String pwd=request.getParameter("userPwd"); out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); out.println("<userinfo><ss>"+myname+"</ss><ss>"+pwd+"</ss></userinfo>"); %>
这里是一个返回xml的数据
然后写一个客户端在flex里面。让其读取数据返回的数据。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script> <!--[CDATA[ import mx.effects.IAbstractEffect; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import Config.Connect2; private var con:Connect2; private function init():void { con=Connect2.getconnect();//链接网络 } private function senddata():void { if(userName.text!="" && userPwd.text!="") { var para:URLVariables=new URLVariables(); para.userName=userName.text; para.userPwd=userPwd.text; con.sendMessage(para,Connect2.myurl);//发送数据 con.addEventListener(ResultEvent.RESULT,resulthander);//监听返回结果 con.addEventListener(FaultEvent.FAULT,faulthander);//监听错误结果 } } private function resulthander(event:ResultEvent):void { trace("d"); var myxml:XML=XML(event.result); trace(myxml);//输出xml格式 trace(myxml.child("ss").children()[0]); } private function faulthander(event:FaultEvent):void { trace("cuowu"); } ]]--> </mx:Script> <mx:Panel width="303" height="209" layout="absolute" x="216" y="113"> <mx:Button click="senddata()" label="Send" labelPlacement="left" x="208" y="113"/> <mx:TextInput id="userName" x="66" y="24" width="196"/> <mx:TextInput id="userPwd" x="66" y="67" width="196"/> <mx:Label x="14" y="26" text="Name"/> <mx:Label x="10" y="69" text="Password"/> </mx:Panel> </mx:Application>
输出结果预览:
<userinfo> <ss>s</ss> <ss>s</ss></userinfo>
s
Powered by: 博客园 Copyright © 2024 阳光VIP Powered by .NET 8.0 on Kubernetes