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
- <% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
- <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <HTML xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
- <Title> untitled document </title>
- </Head>
- <Body>
- <P> welcome here 3 </P>
- <%
- String myname = request. getparameter ("name ");
- String Pwd = request. getparameter ("PWD ");
- If (myname! = NULL)
- {
- Out. Print (myname );
- Out. Print (PWD );
- }
- %>
- </Body>
- </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.
- Package
- {
- Import flash.net. urlloader;
- Import flash.net. URLRequest;
- Import flash. Events .*;
- Import flash.net. urlvariables;
- Import flash.net. urlloaderdataformat;
- Import flash.net .*;
- Public class connect
- {
- Private Static Var CONNECT: connect = NULL;
- // URL of the Connection
- Public Static Var myurl: String = "http: // localhost: 8080/flash/index. jsp ";
- Public static function getconnect (): connect
- {
- If (connect = NULL)
- {
- Connect = new connect ();
- }
- Return connect;
- }
- Public Function sendmessage (para: urlvariables, myurl: string): void
- {// Set the variable for sending data
- // Set the data sending Method
- VaR request: URLRequest = new URLRequest ();
- Request. url = myurl;
- Request. method = urlrequestmethod. Post; // POST method
- Request. Data = para;
- // Send data
- VaR Loader: urlloader = new urlloader ();
- Loader. dataformat = urlloaderdataformat. variables; // format of data transmission
- Loader. addeventlistener (event. Complete, completehandler );
- Loader. addeventlistener (ioerrorevent. io_error, iohandler );
- Try
- {
- Loader. Load (request );
- }
- Catch (E: Error)
- {
- Trace ("failed ");
- }
- }
- Private function completehandler (E: Event): void
- {
- Trace ("OK ");
- }
- Private function iohandler (E: Event): void
- {
- Trace ("failed ");
- }
- }
- }
Test: Use the document class for a test. Create a button in the scenario to send data.
- Package
- {
- Import flash. display. Sprite;
- Import flash. Events .*;
- Import flash. display. simplebutton;
- Import flash. Text. textfield;
- Import flash.net .*;
- Public class main extends Sprite
- {
- Private var con: connect;
- Public Function main ()
- {
- Con = connect. getconnect ();
- BTN. addeventlistener (mouseevent. Click, onclick );
- }
- Private function onclick (E: mouseevent): void
- {
- VaR para: urlvariables = new urlvariables ();
- Para. Name = "22222 ";
- Para. Pwd = "222222 ";
- Con. sendmessage (para, connect. myurl );
- }
- }
- }
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:
- Para. Name = "22222 ";
- Para. Pwd = "222222 ";
- String myname = request. getparameter ("name"); // receives the message sent by the client
- 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.