Skip to main content

Flex and Java - HTTP Get Example

Below is Flex code sample to communicate to a Java servlet in the back-end using HTTP Service.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;

[Bindable]
private var xmldata:XML;

private function send_data():void {
httpXmlDataService.send();
}

private function faultHandler(event:FaultEvent):void{
mx.controls.Alert.show(event.fault.message,"Error in getting result");
}
private function resultHandler(event:ResultEvent):void{
xmldata=event.result as XML;
}
private function displayBeforeTax(row:Object,column:DataGridColumn):String{
return row.profit.beforetax;
}
private function displayAfterTax(row:Object,column:DataGridColumn):String{
return row.profit.aftertax;
}
]]>
</mx:Script>

<mx:HTTPService id="httpXmlDataService"
url="http://localhost:8080/flexJavaIntegration/opendesk"
resultFormat="e4x"
result="resultHandler(event)"
fault="faultHandler(event)"
useProxy="false"/>


<mx:Panel x="43" y="10" width="588" height="369" layout="absolute">
<mx:DataGrid x="50" y="23" width="469" height="265" dataProvider="{xmldata.item}">
<mx:columns>
<mx:DataGridColumn headerText="Type" dataField="@type"/>
<mx:DataGridColumn headerText="Sales" dataField="sales"/>

<mx:DataGridColumn headerText="Profit before tax" labelFunction="displayBeforeTax"/>
<mx:DataGridColumn headerText="Profit after tax" labelFunction="displayAfterTax"/>
</mx:columns>
</mx:DataGrid>

<mx:Button x="66" y="296" label="Get Data" click="send_data()" />
</mx:Panel>
</mx:Application>




Now, here is the Java Servlet.


package com.opendesk.flexjava;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OpenDeskService extends HttpServlet {

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

Enumeration en = req.getParameterNames();

while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = (String) req.getParameter(key);

System.out.println("Key : " + key + " Value : " + value);
}
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

response.setContentType("text/xml");
PrintWriter writer = response.getWriter();

writer.println("<?xml version=\"1.0\"?>\n");
writer.println("<salesdata>");
writer.println("<item type=\"xxxxxxxxxxxxCDs\">");
writer.println("<profit>");
writer.println(" <beforetax>1750</beforetax>");
writer.println(" <aftertax>1600</aftertax>");
writer.println("</profit>");
writer.println("<sales>21000.23</sales>");
writer.println("</item>");
writer.println("<item type=\"DVD\">");
writer.println("<profit>");
writer.println(" <beforetax>1550.34</beforetax>");
writer.println(" <aftertax>1350.10</aftertax>");
writer.println("</profit>");
writer.println("<sales>11010.98</sales>");
writer.println("</item>");
writer.println("</salesdata>");
}
}

Comments

Popular posts from this blog

SAAS Simple Maturity Model

There are two architectural models – commonly referred as SAAS Maturity models- that describe the transition of a service to what is called Multi-tenant efficient, highly scalable application. The SAAS Maturity model described by Microsoft has four distinct stages and is illustrated below. Another similar well-known model for SaaS-maturity is known as Forrester-model but adds another stage known as "Dynamic Business Apps-as-a-service". The three key Attributes of a SAAS Architecture: Configurability: Metadata used to configure the way the application behaves for customers Multi-tenant Efficiency : Maximizing the sharing of resources across tenants Scalability: Maximizing concurrency, resource efficiency SAAS Simple Maturity Model (Microsoft, 2006) SaaS Maturity Model (Forres...

CXF Example –Web Service Using Spring and Maven

Apache CXF is an open source services framework. CXF helps you build Web Services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI. Support for bottom up approach and top down approach. Support for Standards JAX-WS, JSR-181, SAAJ, JAX-RS SOAP 1.1, 1.2, WS-I BasicProfile, WS-Security, WS-Addressing, WS-RM, WS-Policy WSDL 1.1 MTOM Building Web Services – Example 1 Develop a simple Web Service using CXF framework. The example in this case is an InterestRate Service. Tools / technologies Version CXF 2.1 Maven 2.0 Tomcat apache-tomcat-6.0.24 JDK java version 1.6.0_20       The Application Scope The "Interest Rate Service" application demonstrates how easily you can develop a Web Service using CXF frame work. The WSDL service definition defines three operations. Below is the InterestRateService interface.   @WebS...

CXF Example –Web Service Client Using Spring

Continuing from my previous post on (CXF Example –Web Service Using Spring and Maven) , let's create a client application to consume the web service. Steps Generate POJOs from WSDL to access the Web Service using the tool wsdl2java as below. You can access the WSDL used in this example from here.   wsdl2java rateServiceWSDL.xml   WSDL2Java is a command line tool that generates Java classes from an existing WSDL document. Generated classes represent client stubs, server skeletons and data types that will helps you to write client side and server Java programs for Web services defined in the WSDL document.     Create a client-beans.xml as below: < beans xmlns = "http://www.springframework.org/schema/beans"      xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws = "http://cxf.apache.org/jaxws"      xsi:schemaLocation = " http://www.springframework.org/schema/beans http://www.springfram...