Java flex servlet interactive communication

Source: Internet
Author: User

I have been learning flex for more than ten days since the call center project began! Now, I have basically entered the status. Today, I am here to make an example of using Flex to communicate with the backend servlet, hoping to help some new users who just learned flex. To figure out how Flex communicates with the background, I did nothing five days before I started to learn Flex. Today, I am posting this post to help the following brothers avoid detours.

Interaction principle: the three letters --------- XML, the client receives XML data sent from the server.

In this example, the client is a DataGrid component to display the data transmitted by the server. The corresponding mxml file is as follows:

XML Code

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute">
  3. <Mx: model source = "http: // localhost: 8080/flex/First" id = "model">
  4. </MX: Model>
  5. <Mx: panel title = "User Information" width = "776" Height = "281" fontsize = "18">
  6. <Mx: DataGrid dataprovider = "{model. User}" width = "748" Height = "231">
  7. <Mx: columns>
  8. <Mx: datagridcolumn datafield = "name" headertext = "user">
  9. </MX: datagridcolumn>
  10. <Mx: datagridcolumn datafield = "PWD" headertext = "password">
  11. </MX: datagridcolumn>
  12. <Mx: datagridcolumn datafield = "school" headertext = "">
  13. </MX: datagridcolumn>
  14. </MX: columns>
  15. </MX: DataGrid>
  16. </MX: Panel>
  17. </MX: Application>

Note the <mx: Model> label. The source attribute specifies a servlet ing. The servlet uses response to write XML to the client.

Read database data and generate an XML file consisting of two classes: firstservlet. java, one is XML. java, the former is a common Servlet used to write XML files, and the latter is used to generate xml files, completed by Java xml api operations. Take a look at the code first.

Firstservlet code Abstract:

Java code

  1. Try {
  2. XML. INIT ();
  3. Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
  4. // ********** Create a database named flex *************//
  5. String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = Flex ";
  6. // *********** Change your username and password information ************//
  7. Con = drivermanager. getconnection (URL, "sa", "135780 ");
  8. Stmt = con. createstatement ();
  9. // Create a table named users in ********** for details about the SQL statement, see Attachment download **********//
  10. Result = stmt.exe cutequery ("select * from users ");
  11. } Catch (exception e ){
  12. E. printstacktrace ();
  13. }
  14. // Important: Set the response format to XML.
  15. Response. setcontenttype ("text/XML ");
  16. Response. setcharacterencoding ("UTF-8 ");
  17. Printwriter out = response. getwriter ();

The above code connects the servlet to the database and sets the response format. below is the key code for writing the XML file:

Java code

  1. While (result. Next ()){
  2. String [] STRs = new string [3];
  3. STRs [0] = result. getstring ("name ");
  4. STRs [1] = result. getstring ("PWD ");
  5. STRs [2] = result. getstring ("school ");
  6. // ***** Create xmldocument *******//
  7. XML. Create (STRs );
  8. }
  9. Result. Close ();
  10. Stmt. Close ();
  11. Con. Close ();
  12. // ******* Write the XML file to the client ********//
  13. XML. Output (out );

The XML of the last line is an example of XML. java. Some important methods of XML are as follows:

Java code

  1. /**
  2. * Write the XML document to the output stream
  3. *
  4. * @ Param out
  5. * ---- Specified output stream
  6. * @ Throws exception
  7. *
  8. * Prepared by: Wang jinghui Hunan Agricultural University & Hunan airuijie Investment Management Company
  9. */
  10. Public void output (writer) throws exception {
  11. Transformer trans = transformerfactory. newinstance (). newtransformer ();
  12. Trans. setoutputproperty (outputkeys. encoding, "UTF-8 ");
  13. Source source = new domsource (document );
  14. Result result = new streamresult (writer );
  15. Trans. Transform (source, result );
  16. Writer. Flush ();
  17. Writer. Close ();
  18. }

Java code

  1. /**
  2. * Create an XML document
  3. *
  4. * @ Param STRs
  5. * ------- Transmitted name and password and school Parameters
  6. *
  7. * Prepared by: Wang jinghui Hunan Agricultural University & Hunan airuijie Investment Management Company
  8. */
  9. Public void create (string [] STRs ){
  10. // ****** Level 1 subnode ******//
  11. Element first = Document. createelement ("user ");
  12. Root. appendchild (first );
  13. For (INT I = 0; I <STRs. length; I ++ ){
  14. If (I = 0 ){
  15. // ******* Level 2 subnode ******//
  16. Element name = Document. createelement ("name ");
  17. Name. appendchild (document. createtextnode (STRs [I]);
  18. First. appendchild (name );
  19. } If (I = 1 ){
  20. Element Pwd = Document. createelement ("PWD ");
  21. PWD. appendchild (document. createtextnode (STRs [I]);
  22. First. appendchild (PWD );
  23. } If (I = 2 ){
  24. Element school = Document. createelement ("school ");
  25. School. appendchild (document. createtextnode (STRs [I]);
  26. First. appendchild (school );
  27. }
  28. }
  29. }

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.