flashbuilder4.6, Myeclipse10
Reference: http://www.cnblogs.com/lovemoon714/archive/2012/05/25/2517684.html
1. Flex-Side Code
1<?xml version="1.0"encoding="Utf-8"?>2<s:application xmlns:fx="http://ns.adobe.com/mxml/2009" 3xmlns:s="Library://ns.adobe.com/flex/spark" 4xmlns:mx="LIBRARY://NS.ADOBE.COM/FLEX/MX"Minwidth="955"minheight=" the"5Initialize="Init ()">6<fx:Declarations>7<!--place non-visual elements (such as services, value objects) here--8</Fx:declarations>9<fx:Script>Ten<![cdata[ One ImportMx.controls.Alert; A Importmx.rpc.events.FaultEvent; - Importmx.rpc.events.ResultEvent; - ImportMx.rpc.http.HTTPService; the - Private functionInit ():void{ - - //load the Httpservice back listener + Httpser.addeventlistener (resultevent.result,httpserresulthandler); - Httpser.addeventlistener (faultevent.fault,httpserfaulthandler); + } A at Private functionLogin ():void{ - httpser.send (); - } - - //Return Success Events - Private functionHttpserresulthandler (event:resultevent):void{ inAlert.show (Event.result.toString (),"Login Prompt"); - } to //Return Failure Events + Private functionHttpserfaulthandler (event:faultevent):void{ -Alert.show (Event.fault.message as String,"Login Prompt"); the } * $]]>Panax Notoginseng</Fx:script> -<fx:Declarations> the<s:httpservice id="Httpser"Url="HTTP://LOCALHOST:8080/JAVAFLEXHTTP/FLEXLOGINSERVELT1"Method="POST"> +<s:request> A<!--the parameter name, and value, to be sent to the server, which must be consistent when receiving parameter names-- the<userName>{txt_userName.text}</Username> +<userPwd>{txt_userPwd.text}</Userpwd> -</S:request> $</S:httpservice> $</Fx:declarations> -<s:panel x="Panax Notoginseng"y="Max"Width="+"height=" the"> -<s:label x="Panax Notoginseng"y=" "text="User name:"/> the<s:label x="Panax Notoginseng"y=" the"text="Password:"/> -<s:textinput id="Txt_username"x="The "y="+"/>Wuyi<s:textinput id="Txt_userpwd"x=" the"y=" the"displayaspassword="true"/> the<s:button x=" the"y=" the"Label="Login"Id="Btn_login"click="Login ()"/> -</S:panel> Wu</S:application>
FLEX Project Structure:
2. Java-side code
1 Packagecom;2 Importjava.io.IOException;3 ImportJava.io.PrintWriter;4 5 Importjavax.servlet.ServletException;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten One Public classFlexloginserveltextendsHttpServlet { A Public voidDoPost (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { - theResponse.setcontenttype ("text/html"); -Response.setcontenttype ("Text/xml;charset=utf-8"); -PrintWriter out =Response.getwriter (); - //receive parameters from the flex side +String username=request.getparameter ("UserName"); -String userpwd=request.getparameter ("Userpwd"); +String result= "Login failed, user name or password error!" "; A if("admin". Equals (userName) | | "". Equals (USERPWD)) { atresult= "Login Successful! "; - } - //returning login information to the client - out.println (result); - Out.flush (); - out.close (); in } -}
Java Project structure:
3. Web. xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <Display-name></Display-name> <servlet> <Servlet-name>111</Servlet-name> <!--3 can be arbitrarily named to be the same as 2 - <Servlet-class>Com. Flexloginservelt</Servlet-class> <!--4 file directory, note Add package name - </servlet> <servlet-mapping> <Servlet-name>111</Servlet-name> <!--2 can be arbitrarily named to be the same as 3 - <Url-pattern>/flexloginservelt1</Url-pattern> <!--1 can be arbitrarily named - </servlet-mapping> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list> <!--The access order is 1->2->3->4, where the values of 2 and 3 must be the same. The value in the Url-pattern tag is the URL to be entered in the browser's address bar, which can be named by itself, which accesses a servlet named Servlet-name, and the value of two servlet-name tags must be the same as The servlet-name tag in the servlet tag is mapped to the value in the Servlet-class tag, and the class in the Servlet-class tag is eventually accessed. Also, Web. XML/Represents the HTTP://LOCALHOST:8080/+ project name and the. JSP page of the action, plus/representing the absolute path http://localhost:8080/, no plus/representing a relative path -</Web-app>
Results:
The Httpservice of Flex and Java communication