As 3.0 communication with JSP (1)

Source: Internet
Author: User

In order to implement the communication between AS and JSP, we first configure Tomcat (not mentioned here), in the Apache Software Foundation/tomcat 5.5/webapps of Tomcat

Create a directory

Example: J:/program files/Apache Software Foundation/tomcat 5.5/webapps/flash

 

Create an index. jsp file in the Flash directory. Write code for JSP to receive information sent from the client

  1. <% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
  2. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  4. <Head>
  5. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
  6. <Title> untitled document </title>
  7. </Head>
  8. <Body>
  9. <P> welcome here 3 </P>
  10. <%
  11. String myname = request. getparameter ("name ");
  12. String Pwd = request. getparameter ("PWD ");
  13. If (myname! = NULL)
  14. {
  15. Out. Print (myname );
  16. Out. Print (PWD );
  17. }
  18. %>
  19. </Body>
  20. </Html>

Here we only receive two parameters sent by the client, one is name and the other is PWD.

 

Client design:

To connect to the server, we can try to write a connection class on the flash client to connect to the server and send data to JSP. the JSP page can process data.

  1. Package
  2. {
  3. Import flash.net. urlloader;
  4. Import flash.net. URLRequest;
  5. Import flash. Events .*;
  6. Import flash.net. urlvariables;
  7. Import flash.net. urlloaderdataformat;
  8. Import flash.net .*;
  9. Public class connect
  10. {
  11. Private Static Var CONNECT: connect = NULL;
  12. // URL of the Connection
  13. Public Static Var myurl: String = "http: // localhost: 8080/flash/index. jsp ";
  14. Public static function getconnect (): connect
  15. {
  16. If (connect = NULL)
  17. {
  18. Connect = new connect ();
  19. }
  20. Return connect;
  21. }
  22. Public Function sendmessage (para: urlvariables, myurl: string): void
  23. {// Set the variable for sending data
  24. // Set the data sending Method
  25. VaR request: URLRequest = new URLRequest ();
  26. Request. url = myurl;
  27. Request. method = urlrequestmethod. Post; // POST method
  28. Request. Data = para;
  29. // Send data
  30. VaR Loader: urlloader = new urlloader ();
  31. Loader. dataformat = urlloaderdataformat. variables; // format of data transmission
  32. Loader. addeventlistener (event. Complete, completehandler );
  33. Loader. addeventlistener (ioerrorevent. io_error, iohandler );
  34. Try
  35. {
  36. Loader. Load (request );
  37. }
  38. Catch (E: Error)
  39. {
  40. Trace ("failed ");
  41. }
  42. }
  43. Private function completehandler (E: Event): void
  44. {
  45. Trace ("OK ");
  46. }
  47. Private function iohandler (E: Event): void
  48. {
  49. Trace ("failed ");
  50. }
  51. }
  52. }

Test: Use the document class for a test. Create a button in the scenario to send data.

  1. Package
  2. {
  3. Import flash. display. Sprite;
  4. Import flash. Events .*;
  5. Import flash. display. simplebutton;
  6. Import flash. Text. textfield;
  7. Import flash.net .*;
  8. Public class main extends Sprite
  9. {
  10. Private var con: connect;
  11. Public Function main ()
  12. {
  13. Con = connect. getconnect ();
  14. BTN. addeventlistener (mouseevent. Click, onclick );
  15. }
  16. Private function onclick (E: mouseevent): void
  17. {
  18. VaR para: urlvariables = new urlvariables ();
  19. Para. Name = "22222 ";
  20. Para. Pwd = "222222 ";
  21. Con. sendmessage (para, connect. myurl );
  22. }
  23. }
  24. }

When we press the button, we can send data to the JSP page. When the JSP page receives the data, the string content of name and PWD will be displayed:

  1. Para. Name = "22222 ";
  2. Para. Pwd = "222222 ";
  3. String myname = request. getparameter ("name"); // receives the message sent by the client
  4. String Pwd = request. getparameter ("PWD"); // receives the message sent by the client

With this connection class, we can try to do more interaction, such as sending data to the server, and the server returns some XML or other data to the client to achieve the communication effect.

Here is just a simple test.

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.