Java与Flex学习笔记(5)----Flex与Java通信之HttpService方式_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java与Flex学习笔记(5)----Flex与Java通信之HttpService方式

Java与Flex学习笔记(5)----Flex与Java通信之HttpService方式

 2012/4/23 12:40:39  xpp02  程序员俱乐部  我要评论(0)
  • 摘要:Flex用RemoteObject方式与Java通信是最常用的方式,这是一种最直观的方式。当然Flex也可以用HttpService与服务器类如servlet通信,这也是本次学习的重点。这次学习是在上节的基础上进行的。本节学习用到的LoginEvent.as,LoginModule.mxml文件代码如上节所示。好了,新建一个servlet类LoginServlet.java,代码如下所示:[java]viewplaincopyprint?packagecom.yqsn.servlet
  • 标签:笔记 Service 学习 Java 方式 学习笔记 HTTP Flex

? ? Flex用RemoteObject方式与Java通信是最常用的方式,这是一种最直观的方式。当然Flex也可以用HttpService与服务器类如servlet通信,这也是本次学习的重点。


? ? ? ? ?这次学习是在上节的基础上进行的。本节学习用到的LoginEvent.as,LoginModule.mxml文件代码如上节所示。

好了,新建一个servlet类LoginServlet.java,代码如下所示:


[java]?view plaincopyprint?
  1. package?com.yqsn.servlet;??
  2. ???
  3. ???
  4. import?java.io.IOException;??
  5. import?java.io.PrintWriter;??
  6. ???
  7. import?javax.servlet.ServletException;??
  8. import?javax.servlet.http.HttpServlet;??
  9. import?javax.servlet.http.HttpServletRequest;??
  10. import?javax.servlet.http.HttpServletResponse;??
  11. ???
  12. public?class?LoginServlet?extends?HttpServlet?{??
  13. ????@Override??
  14. ????protected?void?service(HttpServletRequest?req,?HttpServletResponse?resp)??
  15. ???????????throws?ServletException,?IOException?{??
  16. ???????//?TODO?Auto-generatedmethod?stub??
  17. ???????req.setCharacterEncoding("utf-8");??
  18. ???????resp.setCharacterEncoding("utf-8");??
  19. ???????PrintWriter?out=resp.getWriter();??
  20. ???????String?username=req.getParameter("username");??
  21. ???????String?passworld=req.getParameter("passworld");??
  22. ???????//System.out.println(username+":"+passworld);??
  23. ???????if(username.equals("admin")&&?passworld.equals("123")){??
  24. ???????????out.print(true);??
  25. ???????}else{??
  26. ???????????out.print(false);??
  27. ???????}??
  28. ????}??
  29. }??


?

?

?

? ? ? ? 这个servlet在web.xml中的配置如下所示:


?

[html]?view plaincopyprint?
  1. <servlet>??
  2. ????<servlet-name>LoginServlet</servlet-name>??
  3. ????<servlet-class>com.yqsn.servlet.LoginServlet</servlet-class>??
  4. ????</servlet>??
  5. <servlet-mapping>??
  6. ????<servlet-name>LoginServlet</servlet-name>??
  7. ????<url-pattern>/LoginServlet</url-pattern>??
  8. ??</servlet-mapping>??


?

? ? ? ?将MyEclipse切换到flash视图,新建一个application文件HttpServiceDemo.mxml,代码如下所示:


?

[html]?view plaincopyprint?
  1. <?xmlversionxmlversion="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"?minWidth="955"?minHeight="600">??
  5. ?????
  6. ????<fx:Script>??
  7. ???????<![CDATA[?
  8. ???????????import?com.flex.ases.LoginEvent;?
  9. ???????????import?com.flex.ases.LoginMess;?
  10. ???????????import?com.flex.module.LoginModule;?
  11. ???????????
  12. ???????????import?mx.controls.Alert;?
  13. ???????????import?mx.managers.PopUpManager;?
  14. ???????????import?mx.rpc.events.FaultEvent;?
  15. ???????????import?mx.rpc.events.ResultEvent;?
  16. ???????????private?var?loginModule:LoginModule=new?LoginModule();?
  17. ???????????[Bindable]?
  18. ???????????private?var?username:String;?
  19. ???????????[Bindable]?
  20. ???????????private?varpassworld:String;?
  21. ???????????protected?function?login_clickHandler(event:MouseEvent):void?
  22. ???????????{?
  23. ??????????????//?TODOAuto-generated?method?stub?
  24. ???????????????PopUpManager.addPopUp(loginModule,this,true);?
  25. ???????????????PopUpManager.centerPopUp(this.loginModule);?
  26. ???????????????loginModule.addEventListener(LoginEvent.LOGIN_EVENT,loginHander);?
  27. ???????????}?
  28. ???????????public?function?loginHander(event:LoginEvent):void{?
  29. ??????????????//varobj:Object=event.loginMess?as?Object;?
  30. ??????????????username=event.loginMess['username'];?
  31. ??????????????passworld=event.loginMess['passworld'];?
  32. ??????????????httpServiceSend.send();?
  33. ???????????}?
  34. ???????????
  35. ???????????protected?function?httpServiceSend_faultHandler(event:FaultEvent):void?
  36. ???????????{?
  37. ??????????????//?TODOAuto-generated?method?stub?
  38. ??????????????Alert.show(event.fault.message?as?String,"提示");?
  39. ??????????????
  40. ???????????}?
  41. ???????????
  42. ???????????protected?function?httpServiceSend_resultHandler(event:ResultEvent):void?
  43. ???????????{?
  44. ??????????????//?TODOAuto-generated?method?stub?
  45. ??????????????var?result:Boolean=event.result?as?Boolean;?
  46. ??????????????if(result==true){?
  47. ??????????????????Alert.show(username+",欢迎您回来!","提示");?
  48. ??????????????????aaa.text=username+",欢迎您回来!";?
  49. ??????????????????login.label="";?
  50. ??????????????????bbb.text="";?
  51. ??????????????????
  52. ??????????????}else{?
  53. ??????????????????Alert.show("对不起,用户名或密码不存在!","提示");?
  54. ??????????????}?
  55. ??????????????//Alert.show("成功了!");?
  56. ???????????}?
  57. ???????????
  58. ???????]]>??
  59. ????</fx:Script>??
  60. ?????
  61. ????<fx:Declarations>??
  62. ???????<!--?Place?non-visualelements?(e.g.,?services,?value?objects)?here?-->??
  63. ???????<s:HTTPService?id="httpServiceSend"??url="http://localhost:8000/JavaAndFlexDemo/LoginServlet"?useProxy="false"fault="httpServiceSend_faultHandler(event)"result="httpServiceSend_resultHandler(event)"?>??
  64. ???????????<s:request?>??
  65. ??????????????<username>{username}</username>??
  66. ???????????????<passworld>{passworld}</passworld>??
  67. ???????????</s:request>??
  68. ???????</s:HTTPService>??
  69. ????</fx:Declarations>??
  70. ????<s:Label?x="200"?y="150"?width="182"?height="27"?fontSize="18"?id="aaa"?text="您还没有登陆,现在就"?verticalAlign="middle"/>??
  71. ????<mx:LinkButton?x="393"??y="150"?width="57"??height="27"?label="登陆"?id="login"?fontSize="18"click="login_clickHandler(event)"/>??
  72. ????<s:Label?x="459"?y="150"?width="37"?height="27"?id="bbb"?fontSize="18"?text="吧!"?verticalAlign="middle"/>??
  73. </s:Application>??


?

?

? ? ? ? ?从代码中我们可以看出,我们先定义一个HttpServlet发送请求httpServiceSend,然后通过下面这种方式存值:


?

[html]?view plaincopyprint?
  1. <s:request?>??
  2. ??????????????<username>{username}</username>??
  3. ??????????????<passworld>{passworld}</passworld>??
  4. ???????????</s:request>??


?

? ? ? ? ?这种方式很简单,我们在后台通过request. getParameter(“参数名”)取值就可以了,当然我们也可以在loginHander(event:LoginEvent)函数中通过下面方式存值并发送请求:


[html]?view plaincopyprint?
  1. public?functionloginHander(event:LoginEvent):void{??
  2. ??????????????//varobj:Object=event.loginMess?as?Object;??
  3. ??????????????username=event.loginMess['username'];??
  4. ??????????????passworld=event.loginMess['passworld'];??
  5. ??????????????var?obj:Object=new?Object;??
  6. ??????????????obj.username=username;??
  7. ??????????????obj.passworld=passworld;??
  8. ??????????????httpServiceSend.send(obj);??
  9. ???????????}??


?

?

? ? 运行结果是一样的,你可以试试。


? ? 好了,程序算是完成了,现在开始验收结果。


?

打开服务器并部署项目,运行felx页面RemoteObjectDemo.mxml,如下所示:




?

? ? ? ? 当我们点击“登陆”按钮后,弹出module页面,如下所示:



?


? ? ? 当我们输入的用户名和密码都正确时则提示你登陆正确:





?

? ? ? ?输入错误则提示你输入不正确:




?

? ? ? 可以看出,我们输入的用户名与密码已经用httpservice方式发送到后台并且成功接受了并将结果返回给前台了。


? ? ? ?好了,就学习这么多,下节将学习WebService方式。

发表评论
用户名: 匿名