Flash and JSP communication Encapsulation

Source: Internet
Author: User

Today I wrote a communication class that enables data interaction with JSP. Paste them together. If you are interested, try again.

Package <br/> {<br/> // something used to communicate with the web page <br/> Import flash.net. urlloader; <br/> Import flash.net. URLRequest; <br/> Import flash. events. *; <br/> Import flash.net. urlvariables; <br/> Import flash.net. urlloaderdataformat; <br/> Import flash.net. *; <br/> public class connection extends eventdispatcher <br/> {<br/> Public Function connection () <br/>{</P> <p >}< br/> Public Function sendmessage (para: urlvariables, myurl: String, type: String = "Post", format: string = "variables"): void <br/>{// sets the variable for sending data <br/> // sets the method for sending data <br/> var request: URLRequest = new URLRequest (); <br/> request. data = para; <br/> request. url = myurl; <br/> switch (type) <br/>{< br/> case "Post": <br/> request. method = urlrequestmethod. post; // sending method <br/> break; </P> <p> case "get": <br/> request. method = urlrequestmethod. get; // sending method <br/> break; <br/>}< br/> // send data <br/> var Loader: urlloader = new urlloader (); <br/> switch (Format) <br/> {<br/> case "variables": <br/> loader. dataformat = urlloaderdataformat. variables; // Data Transmission Format <br/> break; <br/> case "bin": <br/> loader. dataformat = urlloaderdataformat. binary; // format of data sending <br/> break; </P> <p> case "text": <br/> loader. dataformat = urlloaderdataformat. text; // format of data transmission <br/> break; <br/>}< br/> loader. addeventlistener (event. complete, completehandler); <br/> loader. addeventlistener (ioerrorevent. io_error, iohandler); <br/> try <br/>{< br/> loader. load (request); <br/>}< br/> catch (E: Error) <br/>{< br/> trace ("failed "); <br/>}< br/> private function completehandler (E: Event): void <br/>{< br/> // process the returned results, if the database is successfully inserted, You can <br/> var EVT: connectionevent = new connectionevent (connectionevent. complete); <br/> evt.data‑urlloader(e.tar get); <br/> This. dispatchevent (EVT); <br/>}< br/> private function iohandler (E: Event): void <br/>{< br/> This. dispatchevent (New connectionevent (connectionevent. wrong); <br/>}< br/>}

Contains an event class.

 

Package <br/> {<br/> Import flash. events. event; <br/> Import flash.net. *; <br/> public class connectionevent extends event <br/>{< br/> Public static const complete: String = "complete"; <br/> Public var data: urlloader; <br/> Public static const wrong: String = "wrong"; <br/> Public Function connectionevent (type: String, bubbles: Boolean = false, cancelable: boolean = false) <br/>{< br/> super (type, false, false); <br/>}< br/> override public function clone (): event <br/>{< br/> return New connectionevent (loadevent. loadxml); <br/>}< br/>}

 

 

Usage: The following is a simple message board function. You can send messages to JSP files. Use JSP files and MySQL to insert data.

 

Package <br/> {<br/> Import flash. display. *; <br/> Import flash. events. *; <br/> Import flash.net. *; <br/> Import flash. text. *; <br/> Import flash. utils. bytearray; <br/> public class liuyan extends movieclip <br/> {<br/> private var para: urlvariables; <br/> private var Conn: connection; <br/> Public Function liuyan () <br/>{< br/> sendbtn. addeventlistener (mouseevent. mouse_down, ondown); <br/> cancelbtn. addeventl Istener (mouseevent. mouse_down, oncancel); <br/>}< br/> private function oncancel (Event: mouseevent): void <br/>{< br/> name. TEXT = ""; <br/> sex. TEXT = ""; <br/> mail. TEXT = ""; <br/> telephone. TEXT = ""; <br/> MSG. TEXT = ""; <br/>}< br/> private function ondown (Event: mouseevent): void <br/>{< br/> send (); <br/>}< br/> private function send (): void <br/>{< br/> If (name. text! = "" & Sex. Text! = "" & Mail. Text! = "" & Telephone. Text! = "" & MSG. Text! = "") <Br/>{< br/> var para: urlvariables = new urlvariables (); <br/> para. name = Name. text; <br/> para. sex = sex. text; <br/> para. mail = Mail. text; <br/> para. telephone = telephone. text; <br/> para. MSG = MSG. text; <br/> conn = new connection (); <br/> Conn. addeventlistener (connectionevent. complete, completehander); <br/> Conn. addeventlistener (connectionevent. wrong, wronghandler); <br/> Conn. sendmessage (para, connectionurl. liuyanurl); <br/>}< br/> else <br/>{< br/> var worngmsg: textfield = new textfield (); <br/> worngmsg. TEXT = "sorry, the message is incorrect"; <br/> worngmsg. X = 400; <br/> worngmsg. y = 190; <br/> worngmsg. textcolor = 0 xffffff; <br/> addchild (worngmsg); <br/>}</P> <p> private function completehander (Event: connectionevent ): void <br/>{< br/> var para: urlloader = event. data; // process the returned information <br/> trace (para. data); <br/> trace (para. data. success); </P> <p >}</P> <p> private function wronghandler (Event: connectionevent ): void <br/>{< br/> throw new error ("cuowu"); </P> <p >}< br/>}

 

<% @ Page Language = "Java" pageencoding = "UTF-8" Import = "Java. SQL. * "%> <br/> <% @ page contenttype =" text/html; charset = UTF-8 "%> <br/> <JSP: usebean id = "Bean" class = "com. connectserver "> </jsp: usebean> <br/> <% <br/> connection conn = bean. getconnection (); <br/> string name = request. getparameter ("name"); <br/> string sex = request. getparameter ("sex"); <br/> string mail = request. getparameter ("mail"); <br/> string telephone = request. getparameter ("telephone"); <br/> string MSG = request. getparameter ("MSG"); <br/> system. out. println (name); <br/> string SQL = "insert into liuyan2 (name, sex, mail, telephone, MSG) values ('" + name + "', '"+ sex +"', '"+ mail +"', '"+ telephone +"', '"+ MSG + "')"; <br/> int cout = bean.exe cuteupdate (SQL); <br/> If (cout> 0) <br/>{< br/> out. clear (); <br/> out. print ("Success =" + "OK"); <br/>}< br/> else <br/>{< br/> out. print ("Success =" + "fail"); <br/>}< br/>%> <br/>

 

Note the following: Out. Clear ();
Out. Print ("Success =" + "OK ");

 

We hope to get the output content of JSP for flash player, but if there is no out. Clear () line, we will often see the variable undefine.

This left me confused for a long time. I found an issue on the Internet about undefine when Flash gets the JSP return variable. According to its practice, I can solve my problem. But as for why, this is really confusing.

 

The following is a Java JavaBean encapsulation. the development platform I use is myeclise. The database is MySQL. The following modifications can reduce the development time.

 

Package com; </P> <p> Import Java. SQL. *; <br/> public class connectserver {<br/> private connection con; <br/> private resultset RS; <br/> Private Static string url = "JDBC: mysql: // localhost: 3306/test? Useunicode = true & characterencoding = gb2312 "; <br/> Private Static string usernamer =" root "; <br/> Private Static string Password =" 123 "; <br/> Private Static string diver = "com. mySQL. JDBC. driver "; // load the MySQL driver <br/> Public static connection getconnection () throws sqlexception {<br/> try <br/>{< br/> class. forname (diver); <br/>}< br/> catch (classnotfoundexception e) <br/>{< br/> E. printstacktrace (); <br /> Return NULL; <br/>}< br/> return drivermanager. getconnection (URL, usernamer, password); <br/>}</P> <p> Public resultset executequery (string SQL) {<br/> try <br/> {<br/> con = connectserver. getconnection (); <br/> statement = con. createstatement (resultset. type_scroll_sensitive, resultset. concur_read_only); <br/> rs1_statement.exe cutequery (SQL); <br/>}< br/> catch (sqlexception e) <br/>{< br /> E. printstacktrace (); <br/>}< br/> Return Rs; <br/>}< br/> Public int executeupdate (string SQL) {<br/> int COUNT = 0; <br/> statement = NULL; <br/> try <br/>{< br/> con = connectserver. getconnection (); <br/> Statement = con. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable); <br/> count‑statement.exe cuteupdate (SQL); <br/>}< br/> catch (sqlexception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/> finally <br/>{< br/> try <br/>{< br/> If (statement! = NULL) <br/> statement. Close (); <br/> If (con! = NULL) <br/> con. close (); <br/>}< br/> catch (sqlexception ex) <br/>{< br/> ex. printstacktrace (); <br/>}< br/> return count; <br/>}< br/> Public void freerset (resultset rset) {<br/> try <br/> {<br/> If (RS! = NULL) <br/>{< br/> Rs. close (); <br/> con. close (); <br/>}< br/> catch (exception e) <br/>{< br/> E. printstacktrace (); <br/>}</P> <p >}< br/>

 

 

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.