As 3.0 communication with JSP (5)

Source: Internet
Author: User

This time, we made a simple registration page and used flex to communicate with JSP. According to the 4 practice, flex can use RPC to return XML Information output by JSP.

 

Make a registration page:

 

Including name, password, gender, age, and email

 

 

 

Secondly, we made a simple registration page in flex.

 

How do I write the data sending function during registration? According to the method encapsulated last time. We only need to call its method.

Private function regdata (): void <br/>{</P> <p> var para: urlvariables = new urlvariables (); <br/> para. username = username0.text; <br/> para. userpwd = userpwd0.text; <br/> para. sex = sex. text; <br/> para. age = age. text <br/> para. mail = Mail. text; <br/> con. sendmessage (para, connect2.regurl); // send data <br/> con. addeventlistener (resultevent. result, resulthandre2); // listen to the returned results <br/> con. addeventlistener (faultevent. fault, faulthandre2); // listener error result </P> <p >}< br/> private function resulthandre2 (Event: resultevent ): void <br/> {trace ("second listener"); <br/> // trace (event. result); <br/> var myxml2: xml = XML (event. result); <br/> trace (myxml2); </P> <p >}</P> <p> private function faulthand2 (Event: faultevent ): void <br/> {trace ("cuowu"); </P> <p>}

 

The data sent by the client includes the name, password, gender, age, and email.

 

To receive the data sent by the client on the server:

String myname = request. getparameter ("username"); <br/> string mypwd = request. getparameter ("userpwd"); <br/> string mysex = request. getparameter ("sex"); <br/> int myage = integer. parseint (request. getparameter ("Age"); <br/> string mymail = request. getparameter ("mail ");

Because the age is of the int type, the type must be converted during receiving. Because the request receives a string, you need to perform this operation.

 

Next, the old topic. Connect to the database and insert data items:

Note: When the database is webgame, the inserted Table is admin.

<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" Import = "Java. SQL. * "errorpage =" "%> <br/> <% <br/> response. setcontenttype ("text/XML"); <br/> string myname = request. getparameter ("username"); <br/> string mypwd = request. getparameter ("userpwd"); <br/> string mysex = request. getparameter ("sex"); <br/> int myage = integer. parseint (request. getparameter ("Age"); <br/> string mymail = request. getpa Rameter ("mail"); </P> <p> connection con = NULL; </P> <p> try {<br/> class. forname ("com. mySQL. JDBC. driver "); <br/> string dburl =" JDBC: mysql: // localhost: 3306/webgame? Useunicode = true & characterencoding = gb2312 "; <br/> string dbuser =" root "; <br/> string dbpwd =" 123 "; <br/> con = drivermanager. getconnection (dburl, dbuser, dbpwd); <br/> string SQL = "insert into admin (username, userpwd, sex, age, Mail) values ('"+ myname +"', '"+ mypwd +"', '"+ mysex +"', '"+ myage + "', '"+ mymail +"') "; <br/> statement stmt = con. createstatement (resultset. type_scroll_insensitive, resultset. concur_read_only); <B R/> stmt.exe cuteupdate (SQL); <br/> out. println ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> "); <Br/> out. println ("<userinfo> <SS> success </SS> <SS> success2 </SS> </userinfo> "); <br/>}< br/> catch (exception ex) <br/>{< br/> // out. print ("Connection Failed !! <Br> "+ ex. tostring (); <br/> out. println (" <? XML version =/"1.0/" encoding =/"UTF-8/"?> "); <Br/> out. println ("<userinfo> <SS>" + ex. tostring () + "</SS> <SS> fail </SS> </userinfo> "); <br/>}</P> <p >%> </P> <p>

 

If the request is successful, a successful message is returned. If the request fails, an error occurs.

 

 

The following is the client code:

<? 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 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 (); // link network <br/> Reg. addeventlistener (mouseevent. click, regclick); <br/>}< 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> private function regclick (Event: mouseevent ): void <br/>{< br/> View. selectedchild = regpanel; <br/>}</P> <p> private function back (): void <br/>{< br/> View. selectedchild = loginpanel; </P> <p >}</P> <p> private function regdata (): void <br/>{</P> <p> var para: urlvariables = new urlvariables (); <br/> para. username = username0.text; <br/> para. userpwd = userpwd0.text; <br/> para. sex = sex. text; <br/> para. age = age. text <br/> para. mail = Mail. text; <br/> con. sendmessage (para, connect2.regurl); // send data <br/> con. addeventlistener (resultevent. result, resulthandre2); // listen to the returned results <br/> con. addeventlistener (faultevent. fault, faulthandre2); // listener error result </P> <p >}< br/> private function resulthandre2 (Event: resultevent ): void <br/> {trace ("second listener"); <br/> // trace (event. result); <br/> var myxml2: xml = XML (event. result); <br/> trace (myxml2); </P> <p >}</P> <p> private function faulthand2 (Event: faultevent ): void <br/> {trace ("cuowu"); </P> <p >}</P> <p>] --> <br/> </MX: SCRIPT> <br/> <mx: panel x = "107" Y = "65" width = "381" Height = "303" layout = "absolute"> <br/> <mx: viewstack id = "View" width = "361" Height = "263"> <br/> <mx: canvas id = "loginpanel"> <br/> <mx: textinput id = "username" x = "123.5" Y = "60" width = "196"/> <br/> <mx: label x = "78.5" Y = "62" text = "name"/> <br/> <mx: textinput id = "userpwd" x = "123.5" Y = "90" width = "196"/> <br/> <mx: label x = "59.5" Y = "92" text = "password"/> <br/> <mx: button click = "senddata () "label =" login "labelplacement =" Left "x =" 189.5 "Y =" 120 "/> <br/> <mx: label id = "Reg" x = "159.5" Y = "187" text = "Click here toregister" width = "151"/> <br/> <mx: button x = "253.5" Y = "120" label = "button" width = "57"/> <br/> </MX: canvas> <br/> <mx: canvas id = "regpanel"> <br/> <mx: textinput id = "sex" x = "105" Y = "125" width = "196"/> <br/> <mx: label x = "60" Y = "124" text = "sex"/> <br/> <mx: label x = "60" Y = "162" text = "E-mail"/> <br/> <mx: textinput id = "mail" x = "105" Y = "160" width = "196"/> <br/> <mx: textinput id = "username0" x = "105" Y = "36" width = "196"/> <br/> <mx: label x = "60" Y = "38" text = "name"/> <br/> <mx: textinput id = "userpwd0" x = "105" Y = "66" width = "196"/> <br/> <mx: label x = "41" Y = "68" text = "password"/> <br/> <mx: button x = "154" Y = "213" label = "register" Click = "regdata ()"/> <br/> <mx: button id = "btnback" x = "247" Y = "213" label = "back" Click = "back ()"/> <br/> <mx: label x = "60" Y = "98" text = "Age"/> <br/> <mx: textinput x = "105" Y = "96" width = "196" id = "Age"/> </P> <p> </MX: canvas> <br/> </MX: viewstack> <br/> </MX: Panel> <br/> </MX: Application> <br/>

 

 

Note: The registration here does not use regular expressions to regulate the input, so the accuracy of registration information should be improved.

Secondly, the database encapsulation operation can also be encapsulated using JavaBean, which looks a little more flexible.

 

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.