flex與java通訊的小例子二

來源:互聯網
上載者:User

 適合初學者理解

1,testflex.mxml

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx"  
  5.                creationComplete="init()"  
  6.                minWidth="955" minHeight="600">  
  7.     <fx:Declarations>  
  8.         <!-- 將非可視元素(例如服務、值對象)放 在此處 -->  
  9.     </fx:Declarations>  
  10.     <fx:Script>  
  11.         <!--[CDATA[  
  12.             import mx.collections.ArrayCollection;  
  13.             import mx.controls.Alert;  
  14.             import mx.rpc.events.FaultEvent;  
  15.             import mx.rpc.events.ResultEvent;  
  16.             import mx.rpc.http.HTTPService;  
  17.               
  18.             [Bindable]  
  19.             private var selectResponse:String= null;  
  20.             [Bindable]  
  21.             private var arraylist:ArrayCollection = new ArrayCollection([  
  22.                 {name:"CN",data:"中文"},  
  23.                 {name:"US",data:"英文"},  
  24.             ]);  
  25.             private function init():void{  
  26. //              fService.addEventListener(ResultEvent.RESULT,successhandler);  
  27. //              fService.addEventListener(FaultEvent.FAULT,faulthandler);  
  28.             }  
  29.             private function dropdownlistChangeHandler(event:Event = null):void{  
  30.                 var httpservice:HTTPService = new HTTPService();  
  31.                 httpservice.resultFormat = "text";  
  32.                 httpservice.url = "http://localhost:8080/testjava/flexServlet";  
  33.                 httpservice.method = "POST";  
  34.                 var param:Object = new Object;  
  35.                 param.selects = select.selectedItem.data;  
  36.                 httpservice.send(param);  
  37.                 httpservice.addEventListener(ResultEvent.RESULT,successhandler);  
  38. //              fService.sendRequest(select.selectedItem.data);  
  39. //              Alert.show(select.selectedItem.data);  
  40.             }  
  41.             private function successhandler(event:ResultEvent):void{  
  42.                 selectResponse = event.message.body.toString();  
  43.                 languages.text = selectResponse;  
  44. //              Alert.show(selectResponse);  
  45.             }  
  46. //          private function faulthandler(event:FaultEvent):void{  
  47. //              selectResponse = event.message.toString();  
  48. //              languages.text = selectResponse;  
  49. //          }  
  50.         ]]-->  
  51.     </fx:Script>  
  52.     <s:DropDownList x="109" y="66" id="select" labelField="name" dataProvider="{this.arraylist}" change="dropdownlistChangeHandler(event)">  
  53.     </s:DropDownList>  
  54.     <mx:Text id="languages" text="{selectResponse}" x="288" y="198"/>  
  55. </s:Application>  

<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()" minWidth="955" minHeight="600"> <fx:Declarations> <!-- 將非可視元素(例如服務、值對象)放在此處 --> </fx:Declarations> <fx:Script> <!--[CDATA[ import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; [Bindable] private var selectResponse:String= null; [Bindable] private var arraylist:ArrayCollection = new ArrayCollection([ {name:"CN",data:"中文"}, {name:"US",data:"英文"}, ]); private function init():void{ //fService.addEventListener(ResultEvent.RESULT,successhandler); //fService.addEventListener(FaultEvent.FAULT,faulthandler); } private function dropdownlistChangeHandler(event:Event = null):void{ var httpservice:HTTPService = new HTTPService(); httpservice.resultFormat = "text"; httpservice.url = "http://localhost:8080/testjava/flexServlet"; httpservice.method = "POST"; var param:Object = new Object; param.selects = select.selectedItem.data; httpservice.send(param); httpservice.addEventListener(ResultEvent.RESULT,successhandler); //fService.sendRequest(select.selectedItem.data); //Alert.show(select.selectedItem.data); } private function successhandler(event:ResultEvent):void{ selectResponse = event.message.body.toString(); languages.text = selectResponse; //Alert.show(selectResponse); } //private function faulthandler(event:FaultEvent):void{ //selectResponse = event.message.toString(); //languages.text = selectResponse; //} ]]--> </fx:Script> <s:DropDownList x="109" y="66" id="select" labelField="name" dataProvider="{this.arraylist}" change="dropdownlistChangeHandler(event)"> </s:DropDownList> <mx:Text id="languages" text="{selectResponse}" x="288" y="198"/> </s:Application>

2,testjava.java

view plaincopy to clipboardprint?
  1. package servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.io.UnsupportedEncodingException;  
  6.   
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. public class FlexServlet extends HttpServlet {  
  12.     /** 
  13.      *  
  14.      */  
  15.     private static final long serialVersionUID = 1L;  
  16.   
  17.     public FlexServlet() {  
  18.         super();  
  19.     }  
  20.   
  21.     protected void doGet(HttpServletRequest request,  
  22.             HttpServletResponse response) {  
  23.         doPost(request, response);  
  24.     }  
  25.   
  26.     protected void doPost(HttpServletRequest request,  
  27.             HttpServletResponse response) {  
  28.         try {  
  29.             request.setCharacterEncoding("UTF-8");  
  30.         } catch (UnsupportedEncodingException e1) {  
  31.             // TODO Auto-generated catch block  
  32.             e1.printStackTrace();  
  33.         }  
  34.         String selects = request.getParameter("selects");  
  35.         response.setContentType("text/html;charset=UTF-8");  
  36.         PrintWriter pout = null;  
  37.         try {  
  38.             pout = response.getWriter();  
  39.         } catch (IOException e) {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.         }  
  43.         pout.print("選擇:" + selects);  
  44.     }  
  45. }  

package servlet; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FlexServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public FlexServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) { try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String selects = request.getParameter("selects"); response.setContentType("text/html;charset=UTF-8"); PrintWriter pout = null; try { pout = response.getWriter(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } pout.print("選擇:" + selects); } }

3,web.xml

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <servlet>  
  7.         <servlet-name>flexServlet</servlet-name>  
  8.         <servlet-class>servlet.FlexServlet</servlet-class>  
  9.     </servlet>  
  10.     <servlet-mapping>  
  11.         <servlet-name>flexServlet</servlet-name>  
  12.         <url-pattern>/flexServlet</url-pattern>  
  13.     </servlet-mapping>  
  14.     <welcome-file-list>  
  15.         <welcome-file>testflex.html</welcome-file>  
  16.     </welcome-file-list>  
  17. </web-app>  

<?xml version="1.0" encoding="UTF-8"?> <web-app version="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"> <servlet> <servlet-name>flexServlet</servlet-name> <servlet-class>servlet.FlexServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>flexServlet</servlet-name> <url-pattern>/flexServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>testflex.html</welcome-file> </welcome-file-list> </web-app>

 源碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.