Example 2 of flex communication with java

Source: Internet
Author: User

Suitable for beginners

1, testflex. mxml

View plaincopy to clipboardprint?
  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. <! -- Place non-visual elements (such as services and value objects) Here -->
  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. [Bindable]
  18. Private var selectResponse: String = null;
  19. [Bindable]
  20. Private var arraylist: ArrayCollection = new ArrayCollection ([
  21. {Name: "CN", data: "Chinese "},
  22. {Name: "US", data: "English "},
  23. ]);
  24. Private function init (): void {
  25. // FService. addEventListener (ResultEvent. RESULT, successhandler );
  26. // FService. addEventListener (FaultEvent. FAULT, faulthandler );
  27. }
  28. Private function dropdownlistChangeHandler (event: Event = null): void {
  29. Var httpservice: HTTPService = new HTTPService ();
  30. Httpservice. resultFormat = "text ";
  31. Httpservice. url = "http: // localhost: 8080/testjava/flexServlet ";
  32. Httpservice. method = "POST ";
  33. Var param: Object = new Object;
  34. Param. selects = select. selectedItem. data;
  35. Httpservice. send (param );
  36. Httpservice. addEventListener (ResultEvent. RESULT, successhandler );
  37. // FService. sendRequest (select. selectedItem. data );
  38. // Alert. show (select. selectedItem. data );
  39. }
  40. Private function successhandler (event: ResultEvent): void {
  41. SelectResponse = event. message. body. toString ();
  42. Ages. text = selectResponse;
  43. // Alert. show (selectResponse );
  44. }
  45. // Private function faulthandler (event: FaultEvent): void {
  46. // SelectResponse = event. message. toString ();
  47. // Ages. text = selectResponse;
  48. //}
  49. ] -->
  50. </Fx: Script>
  51. <S: DropDownList x = "109" y = "66" id = "select" labelField = "name" dataProvider = "{this. arraylist} "change =" dropdownlistChangeHandler (event) ">
  52. </S: DropDownList>
  53. <Mx: Text id = "Ages" text = "{selectResponse}" x = "288" y = "198"/>
  54. </S: Application>

<? Xml version = "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" creationComplete = "init () "minWidth =" 955 "minHeight =" 600 "> <fx: Declarations> <! -- Place non-visual elements (such as services and value objects) Here --> </fx: Declarations> <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] private var arraylist: ArrayCollection = new ArrayCollection ([{name: "CN", data: "Chinese "}, {name: "US", data: "English"},]); private function init (): void {// fService. addEventListener (ResultEvent. RESULT, successhandler); // fService. addEventListener (FaultEvent. FAULT, faulthandler);} private function dropdownlistChangeHandler (event: Event = null): void {var httpservice: HTTPService = new HTTPService (); httpservice. resultFormat = "text"; httpservice. url = "http: // localhost: 8080/testjava/flexServlet"; httpservice. method = "POST"; var param: Object = new Object; param. selects = select. selectedItem. data; httpservice. send (param); httpservice. addEventListener (ResultEvent. RESULT, successhandler); // fService. sendRequest (select. selectedItem. data); // Alert. show (select. selectedItem. data);} private function successhandler (event: ResultEvent): void {selectResponse = event. message. body. toString (); ages. text = selectResponse; // Alert. show (selectResponse);} // private function faulthandler (event: FaultEvent): void {// selectResponse = event. message. toString (); // ages. text = selectResponse; //}] --> </fx: Script> <s: dropDownList x = "109" y = "66" id = "select" labelField = "name" dataProvider = "{this. arraylist} "change =" dropdownlistChangeHandler (event) "> </s: DropDownList> <mx: text id = "Ages" text = "{selectResponse}" x = "288" y = "198"/> </s: Application>

2. testjava. java

View plaincopy to clipboardprint?
  1. Package servlet;
  2. Import java. io. IOException;
  3. Import java. io. PrintWriter;
  4. Import java. io. UnsupportedEncodingException;
  5. Import javax. servlet. http. HttpServlet;
  6. Import javax. servlet. http. HttpServletRequest;
  7. Import javax. servlet. http. HttpServletResponse;
  8. Public class FlexServlet extends HttpServlet {
  9. /**
  10. *
  11. */
  12. Private static final long serialVersionUID = 1L;
  13. Public FlexServlet (){
  14. Super ();
  15. }
  16. Protected void doGet (HttpServletRequest request,
  17. HttpServletResponse response ){
  18. DoPost (request, response );
  19. }
  20. Protected void doPost (HttpServletRequest request,
  21. HttpServletResponse response ){
  22. Try {
  23. Request. setCharacterEncoding ("UTF-8 ");
  24. } Catch (UnsupportedEncodingException e1 ){
  25. // TODO Auto-generated catch block
  26. E1.printStackTrace ();
  27. }
  28. String selects = request. getParameter ("selects ");
  29. Response. setContentType ("text/html; charset = UTF-8 ");
  30. PrintWriter pout = null;
  31. Try {
  32. Pout = response. getWriter ();
  33. } Catch (IOException e ){
  34. // TODO Auto-generated catch block
  35. E. printStackTrace ();
  36. }
  37. Pout. print ("select:" + selects );
  38. }
  39. }

Package servlet; import java. io. IOException; import java. io. printWriter; import java. io. unsupportedEncodingException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class FlexServlet extends HttpServlet {/*****/private static final long serialVersionUID = 1L; public FlexServlet () {super ();} protected void doGet (HttpServletRequest request, httpServletResponse response) {doPost (request, response);} protected void doPost (HttpServletRequest request, HttpServletResponse response) {try {request. setCharacterEncoding ("UTF-8");} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} String selects = request. getParameter ("selects"); response. setContentType ("text/html; charset = UTF-8"); PrintWriter pout = null; try {pout = response. getWriter ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} pout. print ("select:" + selects );}}

3. web. xml

View plaincopy to clipboardprint?
  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>

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet-name> flexServlet </servlet-name> <servlet-class> servlet. flexServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> flexServlet </servlet-name> <url-pattern>/flexServlet </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> testflex.html </welcome-file> </welcome-file-list> </web-app>

Source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.