Java與Flex學習筆記(5)—-Flex與Java通訊之HttpService方式

來源:互聯網
上載者:User

        

        Flex用RemoteObject方式與Java通訊是最常用的方式,這是一種最直觀的方式。當然Flex也可以用HttpService與伺服器類如servlet通訊,這也是本次學習的重點。


         這次學習是在上節的基礎上進行的。本節學慣用到的LoginEvent.as,LoginModule.mxml檔案代碼如上節所示。


          好了,建立一個servlet類LoginServlet.java,代碼如下所示:


package com.yqsn.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; public class LoginServlet extends HttpServlet {    @Override    protected void 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中的配置如下所示:


<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,代碼如下所示:


<?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;           private var loginModule:LoginModule=new LoginModule();           [Bindable]           private var username:String;           [Bindable]           private varpassworld:String;           protected function login_clickHandler(event:MouseEvent):void           {              // TODOAuto-generated method stub               PopUpManager.addPopUp(loginModule,this,true);               PopUpManager.centerPopUp(this.loginModule);               loginModule.addEventListener(LoginEvent.LOGIN_EVENT,loginHander);           }           public function loginHander(event:LoginEvent):void{              //varobj:Object=event.loginMess as Object;              username=event.loginMess['username'];              passworld=event.loginMess['passworld'];              httpServiceSend.send();           }                     protected function httpServiceSend_faultHandler(event:FaultEvent):void           {              // TODOAuto-generated method stub              Alert.show(event.fault.message as String,"提示");                        }                     protected function httpServiceSend_resultHandler(event:ResultEvent):void           {              // TODOAuto-generated method stub              var result:Boolean=event.result as Boolean;              if(result){                  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,然後通過下面這種方式存值:


<s:request >              <username>{username}</username>              <passworld>{passworld}</passworld>           </s:request>

         這種方式很簡單,我們在後台通過request. getParameter(“參數名”)取值就可以了,當然我們也可以在loginHander(event:LoginEvent)函數中通過下面方式存值並發送請求:


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.dianfusoft.com/showDetail.action?articleId=130410011145 
,更多原創文章,請訪問:http://www.dianfusoft.com/



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.