AS 3.0与jsp通信(6)

问题:我们希望接收jsp发回来的xml信息,并存放在datagrid组件里面去

步骤:

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %&gt; &lt;jsp:useBean id="bean" class="test.Dbconn" scope="page"/&gt; &lt;% response.setContentType("text/xml"); Connection con = null; ResultSet rs=null; con= bean.getConnection(); String sql="select * from admin"; rs=bean.executeQuery(sql); out.println("&lt;?xml version=/"1.0/" encoding=/"utf-8/"?&gt;"); out.println("&lt;userinfo&gt;"); while(rs.next()) { out.println("&lt;username&gt;"); out.println("&lt;name&gt;"+rs.getString(2)+"&lt;/name&gt;"); out.println("&lt;pwd&gt;"+rs.getString(3)+"&lt;/pwd&gt;"); out.println("&lt;sex&gt;"+rs.getString(4)+"&lt;/sex&gt;"); out.println("&lt;age&gt;"+rs.getString(4)+"&lt;/age&gt;"); out.println("&lt;mail&gt;"+rs.getString(5)+"&lt;/mail&gt;"); out.println("&lt;/username&gt;"); } out.println("&lt;/userinfo&gt;"); %&gt; </textarea>

我们使用jsp的javabean 来处理mysql数据库里面查询的信息 并返回给客户端

客户端里面的代码:

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="592" height="444" creationComplete="init()"&gt; &lt;mx:Script&gt; &lt;!--[CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import Config.Connect2; private var conn:Connect2; [Bindable] private var xml:XML; private function init():void { conn=Connect2.getconnect(); } private function senddata():void { trace("dd"); var para:URLVariables=new URLVariables(); conn.sendMessage(para,Connect2.manurl); conn.addEventListener(ResultEvent.RESULT,getData); conn.addEventListener(FaultEvent.FAULT,failHander); } private function getData(event:ResultEvent):void { xml=XML(event.result); trace(xml); } private function failHander(event:FaultEvent):void { trace("cuowu"); } ]]--&gt; &lt;/mx:Script&gt; &lt;mx:Button id="Btn_get" click="senddata()" x="439" y="268" label="Get" width="65"/&gt; &lt;mx:DataGrid x="80" y="58" width="381" dataProvider="{xml.username}"&gt; &lt;mx:columns&gt; &lt;mx:DataGridColumn headerText="姓名" dataField="name"/&gt; &lt;mx:DataGridColumn headerText="密码" dataField="pwd"/&gt; &lt;mx:DataGridColumn headerText="性别" dataField="sex"/&gt; &lt;mx:DataGridColumn headerText="年龄" dataField="age"/&gt; &lt;mx:DataGridColumn headerText="邮件" dataField="mail"/&gt; &lt;/mx:columns&gt; &lt;/mx:DataGrid&gt; &lt;/mx:Canvas&gt; </textarea>

AS 3.0与jsp通信(6)_第1张图片

连接的类中增加链接。用于处理客户端发送的信息

<textarea cols="50" rows="15" name="code" class="c-sharp">package Config { import flash.events.*; import flash.net.*; 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 static var regurl:String="http://localhost:8080/flash/Reg.jsp"; public static var figheurl:String="http://localhost:8080/flash/Fight.jsp"; public static var manurl:String="http://localhost:8080/flash/Manager.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);//发送数据 } } } </textarea>

 

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" width="703" height="543"&gt; &lt;mx:Script&gt; &lt;!--[CDATA[ import View.ManagerPannel; private function init():void { var wnd:ManagerPannel=new ManagerPannel(); addChild(wnd); } ]]--&gt; &lt;/mx:Script&gt; &lt;/mx:Application&gt; </textarea>

原理:jsp输出xml格式,然后返回给客户端,客户端使用datagrid组件来存放jsp 返回的信息。一一对位 里面是返回的xml结果。

<textarea cols="50" rows="15" name="code" class="c-sharp">userinfo&gt; &lt;username&gt; &lt;name&gt;d&lt;/name&gt; &lt;pwd&gt;d&lt;/pwd&gt; &lt;sex&gt;3&lt;/sex&gt; &lt;age&gt;3&lt;/age&gt; &lt;mail&gt;2&lt;/mail&gt; &lt;/username&gt; &lt;username&gt; &lt;name&gt;d&lt;/name&gt; &lt;pwd&gt;2&lt;/pwd&gt; &lt;sex&gt;2&lt;/sex&gt; &lt;age&gt;2&lt;/age&gt; &lt;mail&gt;2&lt;/mail&gt; &lt;/username&gt; &lt;username&gt; &lt;name&gt;jim&lt;/name&gt; &lt;pwd&gt;123&lt;/pwd&gt; &lt;sex&gt;b&lt;/sex&gt; &lt;age&gt;b&lt;/age&gt; &lt;mail&gt;23&lt;/mail&gt; &lt;/username&gt; &lt;username&gt; &lt;name&gt;d&lt;/name&gt; &lt;pwd&gt;d&lt;/pwd&gt; &lt;sex&gt;d&lt;/sex&gt; &lt;age&gt;d&lt;/age&gt; &lt;mail&gt;2&lt;/mail&gt; &lt;/username&gt; &lt;/userinfo&gt; </textarea>

你可能感兴趣的:(AS 3.0与jsp通信(6))