Flex4 with Java communication (two, with servlet communication)

Source: Internet
Author: User

Description: This article describes using Urlrequest+urlloader to communicate with the Java server-side servlet.

URLRequest class

The URLRequest class captures all the information in a single HTTP request.

Urlvariables class

Use the Urlvariables class to transfer variables between applications and servers.

Urlrequestmethod class

The class has only two static constant values, one for post, and one for get, which can be used as an enumeration type to specify a URL request using post or get mode.

Urlloader class

The class that really deals with the service end sends a reuqest to the server through the load method, and you must add a complete listener to the object before load, and receive the information returned by the servers. An IO error may be generated during the transfer process, and you can add a IOError listener to the object if necessary.

Example:
var request:urlrequest = new URLRequest ();

Specify Rquest Address

Request.url= "designated Rquest address";

The default is Urlrequestmethod.get, and if you use a Get method, you can not specify
Request.method = Urlrequestmethod.post;

Creates a parameter variable object that holds all parameter data sent to the service, and the Get method can be used without
var vars:urlvariables = new Urlvariables ();
vars["varName"] = Varvalue; Put the parameter key, the value pair into the VARs object.
vars["var2name"] = Var2value;
Request.data = VARs; Put the parameter object into the request object as a parameter

Create a loader from the request object,
var loader:urlloader = new Urlloader (request);


handling of events and return values

Loader.addeventlistener (//) adds the listener to the loader object when it completes.
Event.complete,
function (e:event): void//temporary definition of a listener
{
The data returned by the server is stored in the loader, and can be obtained by using the following statement
Trace (Urlloader (e.target). data);//Handle the server-side returns in the listener
Complete
Process the data;
}
);

Loader.addeventlistener (//ioerror Listener
Ioerrorevent.io_error,
Temporary listener, processing of IO errors in this function
function (e:ioerrorevent): void {/* process the err;*/}
);
Loader.load (Request)//Send the Request object to the service side. When the request completes, the complete event is automatically triggered

An example is given below:

XML code:

<?xml version= "1.0" encoding= "Utf-8"?> <s:application xmlns:fx= "http://ns.adobe.com/mxml/2009" Library://ns.adobe.com/flex/spark "xmlns:mx=" library://ns.adobe.com/flex/mx "minwidth=" 955 "minHeight=" > <fx:Script> <!--[cdata[import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import Mx.utils.URLUtil; protected function Ok_clickhandler (event:mouseevent): void {var uv:urlvariables = new Urlvariables ("name=zc&age=22 &address=chongqing "); /* can also be this way var vars:urlvariables = new Urlvariables (); vars["varName"] = Varvalue; Put the parameter key, the value pair into the VARs object. vars["var2name"] = Var2value; * * var request:urlrequest = new URLRequest (); Request.url = "Http://localhost:8080/httpdemo/servlet/HttpTestServlet"; Request.method = Urlrequestmethod.post; Request.data = UV; var loader:urlloader = new Urlloader (); Loader.load (Request); Loader.addeventlistener (Event.complete,txtcompletehandler); Response to get Data event method after registration request completes private function Txtcompletehandler (E:evENT): void {var loader:urlloader = Urlloader (e.target);/* Urlutil method interpretation returns an object from a string. Strings contain name=value pairs, which become dynamic properties of the returned object. These properties are delimited by the specified separator. The method converts numbers, Boolean values, arrays (defined by []), and subclasses (defined by "{}"). By default, the URL pattern in the%XX format is converted to the corresponding string character. For example: var s:string = "name=alex;age=21"; var o:object = Urlutil.stringtoobject (S, ";", true); Return object: {name: "Alex", age:21}. *//Direct Get return value content: Urlloader (e.target). data var o:object = Urlutil.stringtoobject (Loader.data, ";", true); Using Urlutil to deserialize the data to facilitate the use of txtName.Text = O.name; Txtage.text = O.age; Txtaddress.text = o.address; Labmsg.alpha = 0; }]]--> </fx:Script> <fx:Declarations> </fx:Declarations> <s:panel x= "y=" width= "287" height= "247" title= "Form" > <s:textinput x= "the" "y=" "id=" "txtaddress" and "/> <s:textinput" x= "y=" "id=" "Y=" id= "txtname"/> "<s:button" x= "<s:textinput x=" txtage "" y= "/>" ", 158" "" "", "" "," "" and "" " ClickHandler (Event) "/> <s:label x=" y= "text=" Name: "/> <s:labEl x= "y=" "text=" Age: "height=" "/> <s:label the x=" "y=" "text=" Address: "height="/> </s:panel& Gt </s:Application>

Java code:

public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Request.setcharacterencoding ("Utf-8"); Gets the parameter returned by the Urlresuest request back data, delimited to facilitate flex serialization of data string rs = new string ("Name=" + request.getparameter ("name") + "; age=" + Re Quest.getparameter ("age") + "; address=" + Request.getparameter ("address"); Response.setcontenttype ("Text/plain"); System.out.println (RS); Response.getwriter (). write (RS); }

OK, run the application, you can:

Background:


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.