Flex与Java通信之HttpService方式
2019獨角獸企業重金招聘Python工程師標準>>>
Flex用RemoteObject方式與Java通信是最常用的方式,這是一種最直觀的方式。當然Flex也可以用HttpService與服務器類如servlet通信,這也是本次學習的重點。
這次學習是在上節的基礎上進行的。本節學習用到的LoginEvent.as,LoginModule.sa文件代碼如上節所示。
好了,新建一個servlet類LoginServlet.java,代碼如下所示:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package?com.it161.servlet; import?java.io.IOException; import?java.io.PrintWriter; import?javax.servlet.ServletException; import?javax.servlet.http.HttpServlet; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; publicclass?LoginServlet?extends?HttpServlet?{ @Override protectedvoid?service(HttpServletRequest?req,?HttpServletResponse?resp) throws?ServletException,?IOException?{ //?TODO?Auto-generatedmethod?stub req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); PrintWriter?out=resp.getWriter(); String?username=req.getParameter("username"); String?passworld=req.getParameter("passworld"); //System.out.println(username+":"+passworld); if(username.equals("admin")&&passworld.equals("123")){ out.print(true); }else{ out.print(false); } } } |
?
這個servlet在web.xml中的配置如下所示:
| 1 2 3 4 5 6 7 8 | <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.yqsn.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> |
將MyEclipse切換到flash視圖,新建一個application文件HttpServiceDemo.mxml,代碼如下所示:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <?xmlversion="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"?minWidth="955"?minHeight="600"> <fx:Script> <![CDATA[ import?com.flex.ases.LoginEvent; import?com.flex.ases.LoginMess; import?com.flex.module.LoginModule; import?mx.controls.Alert; import?mx.managers.PopUpManager; import?mx.rpc.events.FaultEvent; import?mx.rpc.events.ResultEvent; privatevar?loginModule:LoginModule=new?LoginModule(); [Bindable] private?var?username:String; [Bindable] private?varpassworld:String; protectedfunction?login_clickHandler(event:MouseEvent):void { //?TODOAuto-generated?method?stub PopUpManager.addPopUp(loginModule,this,true); PopUpManager.centerPopUp(this.loginModule); loginModule.addEventListener(LoginEvent.LOGIN_EVENT,loginHander); } publicfunction?loginHander(event:LoginEvent):void{ //varobj:Object=event.loginMess?as?Object; username=event.loginMess['username']; passworld=event.loginMess['passworld']; httpServiceSend.send(); } protectedfunction?httpServiceSend_faultHandler(event:FaultEvent):void { //?TODOAuto-generated?method?stub Alert.show(event.fault.message?as?String,"提示"); } protectedfunction?httpServiceSend_resultHandler(event:ResultEvent):void { //?TODOAuto-generated?method?stub var?result:Boolean=event.result?as?Boolean; if(result==true){ Alert.show(username+",歡迎您回來!","提示"); aaa.text=username+",歡迎您回來!"; login.label=""; bbb.text=""; }else{ Alert.show("對不起,用戶名或密碼不存在!","提示"); } //Alert.show("成功了!"); } ]]> </fx:Script> <fx:Declarations> <!--?Place?non-visualelements?(e.g.,?services,?value?objects)?here?--> <s:HTTPService?id="httpServiceSend"??url=" http://localhost:8000/JavaAndFlexDemo/LoginServlet"?useProxy="false"fault="httpServiceSend_faultHandler(event)"result="httpServiceSend_resultHandler(event)"?> <s:request> <username>{username}</username> <passworld>{passworld}</passworld> </s:request> </s:HTTPService> </fx:Declarations> <s:Label?x="200"?y="150"?width="182"?height="27"?fontSize="18"?id="aaa"?text="您還沒有登陸,現在就"?verticalAlign="middle"/> <mx:LinkButton?x="393"??y="150"?width="57"??height="27"?label="登陸"?id="login"?fontSize="18"click="login_clickHandler(event)"/> <s:Label?x="459"?y="150"?width="37"?height="27"?id="bbb"?fontSize="18"?text="吧!"?verticalAlign="middle"/> </s:Application> |
從代碼中我們可以看出,我們先定義一個HttpServlet發送請求httpServiceSend,然后通過下面這種方式存值:
| 1 2 3 4 | <s:request?> <username>{username}</username> <passworld>{passworld}</passworld> </s:request> |
這種方式很簡單,我們在后臺通過request. getParameter(“參數名”)取值就可以了,當然我們也可以在loginHander(event:LoginEvent)函數中通過下面方式存值并發送請求:
| 1 2 3 4 5 6 7 8 9 | public?functionloginHander(event:LoginEvent):void{ //varobj:Object=event.loginMess?as?Object; username=event.loginMess['username']; passworld=event.loginMess['passworld']; var?obj:Object=new?Object; obj.username=username; obj.passworld=passworld; httpServiceSend.send(obj); } |
運行結果是一樣的,你可以試試。
好了,程序算是完成了,現在開始驗收結果。
打開服務器并部署項目,運行felx頁面RemoteObjectDemo.mxml,如下所示:
當我們點擊“登陸”按鈕后,彈出module頁面,如下所示:
當我們輸入的用戶名和密碼都正確時則提示你登陸正確:
輸入錯誤則提示你輸入不正確:
可以看出,我們輸入的用戶名與密碼已經用httpservice方式發送到后臺并且成功接受了并將結果返回給前臺了。
好了,就學習這么多,下節將學習WebService方式。
原創文章,轉載請注明出處:http://www.it161.com/article/webDetail?articleid=140111230433
更多原創內容,請訪問:http://www.it161.com/
轉載于:https://my.oschina.net/dianfusoft/blog/192298
總結
以上是生活随笔為你收集整理的Flex与Java通信之HttpService方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux shell 上传,下载ftp
- 下一篇: [AX]AX2012 AIF(二):文档