This time, we use flex for remote and JSP access, and return XML data values. The following is an encapsulated class that inherits the httpservice class.
Package config <br/>{< br/> Import flash. events. *; <br/> Import flash.net. *; </P> <p> Import MX. RPC. events. faultevent; <br/> Import MX. RPC. events. resultevent; <br/> Import MX. RPC. HTTP. httpservice; <br/> public class connect2 extends httpservice <br/> {<br/> Private Static Var CONNECT: connect2 = NULL; </P> <p> // URL of the connection <br/> Public Static Var myurl: String = "http: // localhost: 8080/flash/receive. JSP "; </P> <p> Public Function connect2 () <br/> {<br/> This. method = "Post"; <br/> This. useproxy = false; // do not start the proxy service <br/> This. resultformat = "XML"; // set the format of the returned data to XML <br/>}</P> <p> Public static function getconnect (): connect2 <br/>{< br/> If (connect = NULL) <br/>{< br/> connect = new connect2 (); <br/>}< br/> return connect; <br/>}< br/> Public Function sendmessage (para: urlvariables, myurl: string ): void <br/> {// set the variable for sending data <br/> This. url = myurl; // specify the JSP page to be sent <br/> This. send (para); // send data <br/>}< br/>
Second: JSP output value
Out. println ("value you want to return"); read through FLEX
<% <Br/> response. setcontenttype ("text/XML"); <br/> string myname = request. getparameter ("username"); <br/> string Pwd = request. getparameter ("userpwd"); </P> <p> out. println ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> "); <Br/> out. println ("<userinfo> <SS>" + myname + "</SS> <SS>" + PWD + "</SS> </userinfo> "); </P> <p> %> </P> <p>
Here is a data that returns XML
Then write a client in flex. Read the data returned by the data.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "Init ()"> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> Import MX. effects. iabstracteffect; <br/> Import MX. RPC. events. faultevent; <br/> Import MX. RPC. events. resultevent; <br/> Import config. connect2; </P> <p> private var con: connect2; </P> <p> private function Init (): void <br/> {<br/> con = connect2.getconnect (); // Connection Network </P> <p >}< br/> private function senddata (): void <br/> {<br/> If (username. text! = "" & Userpwd. Text! = "") <Br/>{< br/> var para: urlvariables = new urlvariables (); <br/> para. username = username. text; <br/> para. userpwd = userpwd. text; <br/> con. sendmessage (para, connect2.myurl); // send data <br/> con. addeventlistener (resultevent. result, resulthander); // listen to the returned results <br/> con. addeventlistener (faultevent. fault, faulthander); // listener error result <br/>}</P> <p> private function resulthander (Event: resultevent ): void <br/> {trace ("D"); </P> <p> var myxml: xml = XML (event. result); <br/> trace (myxml); // output XML format <br/> trace (myxml. child ("SS "). children () [0]); </P> <p >}</P> <p> private function faulthander (Event: faultevent ): void <br/> {trace ("cuowu"); </P> <p >}</P> <p>] --> <br/> </MX: SCRIPT> <br/> <mx: panel width = "303" Height = "209" layout = "absolute" x = "216" Y = "113"> <br/> <mx: button click = "senddata ()" label = "send" labelplacement = "Left" x = "208" Y = "113"/> <br/> <mx: textinput id = "username" x = "66" Y = "24" width = "196"/> <br/> <mx: textinput id = "userpwd" x = "66" Y = "67" width = "196"/> <br/> <mx: label x = "14" Y = "26" text = "name"/> <br/> <mx: label x = "10" Y = "69" text = "password"/> <br/> </MX: Panel> <br/> </MX: application> <br/>
Preview the output result:
<Userinfo>
<SS> S </SS>
<SS> S </SS>
</Userinfo>
S