As 3.0 communication with JSP (6)

Source: Internet
Author: User

Problem: we want to receive XML Information sent from JSP and store it in the DataGrid component.

Steps:

<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" Import = "Java. SQL. * "errorpage =" "%> <br/> <JSP: usebean id =" Bean "class =" test. dbconn "Scope =" page "/> <br/> <% <br/> response. setcontenttype ("text/XML"); </P> <p> connection con = NULL; <br/> resultset rs = NULL; <br/> con = bean. getconnection (); <br/> string SQL = "select * from admin"; <br/> rsw.bean.exe cutequery (SQL); <br/> out. println ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> "); <Br/> out. println ("<userinfo>"); <br/> while (RS. next () <br/>{< br/> out. println ("<username>"); <br/> out. println ("<Name>" + Rs. getstring (2) + "</Name>"); <br/> out. println ("<PWD>" + Rs. getstring (3) + "</pwd>"); <br/> out. println ("<sex>" + Rs. getstring (4) + "</sex>"); <br/> out. println ("<age>" + Rs. getstring (4) + "</age>"); <br/> out. println ("<mail>" + Rs. getstring (5) + "</mail>"); <br/> out. println ("</username>"); <br/>}< br/> out. println ("</userinfo>"); </P> <p >%> <br/>

We use JSP Javabean to process the query information in the MySQL database and return it to the client.

Code in the client:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: canvas xmlns: MX = "http://www.adobe.com/2006/mxml" width = "592" Height = "444" creationcomplete = "Init ()"> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> Import MX. RPC. events. faultevent; <br/> Import MX. RPC. events. resultevent; <br/> Import config. connect2; <br/> private var Conn: connect2; <br/> [Bindable] <br/> private var XML: XML; <br/> private function Init (): void <br/>{< br/> conn = connect2.getconnect (); <br/>}</P> <p> private function senddata (): void <br/> {<br/> trace ("DD"); <br/> var para: urlvariables = new urlvariables (); <br/> Conn. sendmessage (para, connect2.manurl); <br/> Conn. addeventlistener (resultevent. result, getdata); <br/> Conn. addeventlistener (faultevent. fault, failhander); <br/>}< br/> private function getdata (Event: resultevent): void <br/>{< br/> xml = XML (event. result); <br/> trace (XML); <br/>}< br/> private function failhander (Event: faultevent ): void <br/>{< br/> trace ("cuowu "); </P> <p >}</P> <p>] --> <br/> </MX: SCRIPT> <br/> <mx: button id = "btn_get" Click = "senddata () "x =" 439 "Y =" 268 "label =" get "width =" 65 "/> <br/> <mx: dataGrid x = "80" Y = "58" width = "381" dataprovider = "{XML. username} "> <br/> <mx: columns> <br/> <mx: datagridcolumn headertext =" name "datafield =" name "/> <br/> <mx: datagridcolumn headertext = "password" datafield = "PWD"/> <br/> <mx: datagridcolumn headertext = "gender" datafield = "sex"/> <br/> <mx: datagridcolumn headertext = "Age" datafield = "Age"/> <br/> <mx: datagridcolumn headertext = "mail" datafield = "mail"/> <br/> </MX: columns> <br/> </MX: dataGrid> </P> <p> </MX: canvas> <br/>

Add a link to the connected class. Used to process information sent by the client

Package config <br/>{< br/> Import flash. events. *; <br/> Import flash.net. *; </P> <p> 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 "; <br/> Public Static Var regurl: String =" http: /localhost: 8080/flash/Reg. JSP "; <br/> Public Static Var figheurl: String =" http: // localhost: 8080/flash/fight. JSP "; <br/> Public Static Var manurl: String =" http: // localhost: 8080/flash/manager. JSP "; <br/> 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/>

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "Init () "width =" 703 "Height =" 543 "> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> Import view. managerpannel; </P> <p> private function Init (): void <br/> {<br/> var WND: managerpannel = new managerpannel (); <br/> addchild (WND); <br/>}< br/>] --> <br/> </MX: SCRIPT> </P> <p> </MX: Application> <br/>

Principle: JSP outputs the XML format and returns it to the client. The client uses the DataGrid component to store the information returned by JSP. The one-to-one matching result contains the returned XML results.

Userinfo> <br/> <username> <br/> <Name> d </Name> <br/> <PWD> d </pwd> <br/> <sex> 3 </sex> <br/> <age> 3 </age> <br/> <mail> 2 </mail> <br/> </username> <br/> <username> <br/> <Name> d </Name> <br/> <PWD> 2 </pwd> <br/> <sex> 2 </sex> <br/> <age> 2 </age> <br/> <mail> 2 </mail> <br/> </username> <br/> <username> <br/> <Name> Jim </Name> <br/> <PWD> 123 </pwd> <br/> <sex> B </sex> <br/> <age> B </age> <br/> <mail> 23 </mail> <br/> </username> <br/> <name> d </Name> <br/> <PWD> d </pwd> <br/> <sex> d </sex> <br/> <age> d </ age> <br/> <mail> 2 </mail> <br/> </username> <br/> </userinfo> </P> <p>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.