flex与java通信的小例子二

 适合初学者理解

1,testflex.mxml

  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>  

2,testjava.java

  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. }  

3,web.xml

  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>  

 源码下载地址

posted on 2010-05-11 11:53  念时  阅读(524)  评论(0编辑  收藏  举报

细节决定成败!态度决定一切!