Flex中HTTPService與java後臺通訊
阿新 • • 發佈:2019-02-01
方式一,前端mxml檔案
<fx:Declarations> <!--url代表請求路徑,resultFormat代表返回格式,result是宣告回撥函式,其中 myFault(event)的event是Flex自動給我們填充進去的--> <s:HTTPService id="myHttpService" method="GET" resultFormat="text" useProxy="false" fault="myFault(event)" result="myResult(event)" url="http://127.0.0.1:8080/mytest/HttpService"> <!--userName代表引數名稱,{this.username.text}代表id為username的 文字框中的值,括號{}代表的意思是繫結--> <!--<s:request xmlns=""> <userName>張三</userName> <userPwd>zs</userPwd> </s:request>--> </s:HTTPService> </fx:Declarations> <fx:Script> <![CDATA[ import mx.rpc.events.FaultEvent; import mx.controls.Alert; import mx.rpc.events.ResultEvent; private function myFunction():void{ //myHttpService.send(); /*多行註釋*/ var val:Object = new Object(); val.userName = "張三"; val.userPwd = "zs"; myHttpService.send(val); } private function myResult(event:ResultEvent){ var msg = event.result.toString(); Alert.show(msg); } private function myFault(event:FaultEvent){ var msg = event.fault.toString(); Alert.show("錯誤:"+msg); } ]]> </fx:Script> <mx:Button id="myButton" x="160" y="136" width="100" height="40" label="點選" click="myFunction()"/>
後端servlet
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8;"); PrintWriter out = response.getWriter(); //接收請求的userName String userName = request.getParameter("userName"); //get方式處理亂碼 userName = new String(userName.getBytes("ISO-8859-1"),"UTF-8"); //接收請求的password String userPwd = request.getParameter("userPwd"); userPwd = new String(userPwd.getBytes("ISO-8859-1"),"UTF-8"); out.print("你輸入的姓名為:" + userName + " 密碼為:" + userPwd); }
方式二,前端mxml檔案
<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]類似於註解,作用是當arrayList的值一旦發生變化,表單DropDownList //的值也會發生變化 [Bindable] private var arrayList:ArrayCollection = new ArrayCollection([ {name:"CN",data:"中文"},{name:"US",data:"英文"} ]); private function dropdownListChangeHandler(event:Event = null):void{ var httpService:HTTPService = new HTTPService(); httpService.resultFormat = "text"; httpService.url = "http://localhost:8080/mytest/HttpService02"; httpService.method = "POST"; var param:Object = new Object; //object物件.引數名=值 傳值操作 param.selects = select.selectedItem.data; httpService.send(param); httpService.addEventListener(ResultEvent.RESULT,successHandler); httpService.addEventListener(FaultEvent.FAULT,faultHandler); } 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.body.toString(); languages.text = selectResponse; //Alert.show(selectResponse); } ]]> </fx:Script> <s:DropDownList id="select" change="dropdownListChangeHandler(event)" dataProvider="{this.arrayList}" labelField="name" selectedIndex="0"/> <mx:Text id="languages" text=""/>
後端servlet
<span style="white-space:pre"> </span>public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8;");
PrintWriter out = response.getWriter();
String selects = request.getParameter("selects");
out.print("選擇:" + selects);
}
web.xml
<span style="white-space:pre"> </span><servlet>
<servlet-name>HttpService</servlet-name>
<servlet-class>com.lxz.HttpService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpService</servlet-name>
<url-pattern>/HttpService</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HttpService02</servlet-name>
<servlet-class>com.lxz.HttpService02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpService02</servlet-name>
<url-pattern>/HttpService02</url-pattern>
</servlet-mapping>