Flex and Java Communication three: Httpservice

Source: Internet
Author: User

Three ways for flex to communicate with Java:

1. Through Httpservice: You access an XML or servlet, and then flex receives the returned XML data
2. Through WebService: You access a WebService service, and then get the text message
3. Through RemoteObject: The remote Service interface method can be accessed through blazeds, and Flex receives an object.
Build the project: (from Baidu Library, "Flex (Eclipse) Development configuration Manual. pdf")

Flexhttpservice Access defines the Httpservice method in Javaflex:
1. Define a servlet in Javaflex that returns an XML
Package my.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; @SuppressWarnings ("Serial") public class Httpserviceservlet extends HttpServlet {@Override protected void service (Http 
		ServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {resp.setcontenttype ("text/xml");
		Resp.setcharacterencoding ("Utf-8");
		PrintWriter out = Resp.getwriter ();
		Out.println ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>");
		Out.println ("<rows>");
		Out.println ("<row>");
		Out.println ("<id>");
		Out.println ("1");
		Out.println ("</id>");
		Out.println ("<msg>");
		Out.println ("Hello");
		Out.println ("</msg>");
		Out.println ("</row>");
		Out.println ("<row>");
		Out.println ("<id>");
		Out.println ("2"); Out.println ("</id>"));
		Out.println ("<msg>");
		Out.println ("haha");
		Out.println ("</msg>");
		Out.println ("</row>");
		Out.println ("</rows>");
	Out.close ();
 }

}

To configure the servlet in Web.xml:
<servlet>
  <servlet-name>MyHttpServiceServlet</servlet-name>
  <servlet-class> my.servlet.httpserviceservlet</servlet-class>
 </servlet>
 <servlet-mapping>
  < Servlet-name>myhttpserviceservlet</servlet-name>
  <url-pattern>/myhttpserviceservlet</ Url-pattern>
 </servlet-mapping>

2. Define and Access Myhttpserviceservlet in Flexhttpservice:
Defined:
	<fx:Declarations>
		<!--place non-visual elements (such as services, value objects) here-->
		<s:httpservice id= "GetData" method= "POST"
					   url= "Http://localhost:8080/javaFlex/MyHttpServiceServlet" useproxy= "false"/>
	</fx:declarations >
You can also configure it in Proxy-config without specifying a URL: Proxy-config.xml configure Httpservice The URL address to request, and the "destination" mapping relationship in the program to access the URL
Access:
<s:button label= "Get Data" click= "Getdata.send ()"/>

Get the result:
	<mx:datagrid width= "332" height= "160" dataprovider= "{GetData.lastResult.rows.row}" >
3. Results:


---------------------------------------------------------Gorgeous split Line--------------------------------------------------------- ----------------------
The problem with Cross-domain access is not resolved above:
From: http://hi.baidu.com/hakerivan/item/211218d6c6a348866cce3f7f (not validated)

Sand Box Effect Method:

1, using UseProxy properties and crossdomain.xml can be accessed across domains, which is one of the solutions.

2, write a JS or PHP script, with Ajax (XMLHTTP Object) to access the Cross-domain URL.


Web-inf\flex\proxy-config.xml:
<destination id= "Catalog" > 
    <properties> 
            <url>/{context.root}/testdrive-httpservice/ Catalog.jsp</url> 
        </properties> 
    

Other: Flash cross-Domain policy file crossdomain.xml configuration httpservice use useproxy parameters
Related Article

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.