Yesterday I showed you how to get Perl and Flex communicating via XML. Today I’ll do the same thing but this time with Java, in particular a Java Servlet.
For this project I used the following tools:
1. Flex Builder 3 – a free trial is available
2. Netbeans or Eclipse, I happened to use Netbeans 6.5 for this but Eclipse properly configured will work just fine.
3. Make sure you have either Tomcat or Jetty to deploy to. Glassfish should also work.
To begin with I created a new “Web Application” in Netbeans and called it “TalkToFlex”. I chose to deploy it to Tomcat 6.x. Once the wizard had generated the project for me I added a servlet called “TalkXML” and the following code:
package com.giantflyingsaucer;
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;
public class TalkXML extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String message = "NONE";
response.setContentType("text/xml;charset=UTF-8");
// There are much better ways to render XML than
// writing it out like this, I'm doing it this way
// for the sake of simplicity for this tutorial
PrintWriter out = response.getWriter();
try
{
if (request.getParameterMap().containsKey("message"))
message = request.getParameter("message");
out.println("<?xml version=\"1.0\"?>");
out.print("<root>");
out.println("<messages>");
out.println("<msg1>" + message + "</msg1>");
out.println("<msg2>Goodbye World</msg2>");
out.println("</messages>");
out.println("</root>");
}
finally
{
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
First as the comment states you will probably want to render your XML using one of the many XML libraries out there, for simplicity I simply wrote it out manually via the PrintWriter.
All we are doing here is getting a parameter called “message” and then putting it back into the XML that we’ll send back to Flex.
Create a new Flex Builder 3 project and call it “TalkToJava”. I didn’t setup the application server options, you can though if you wish. My MXML is simplistic and looks like this:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function onResult():void
{
txt.htmlText = txt.htmlText + "<br/>Msg1: " + xmlService.lastResult.messages["msg1"];
txt.htmlText = txt.htmlText + "<br/>Msg2: " + xmlService.lastResult.messages["msg2"];
}
]]>
</mx:Script>
<mx:HTTPService id="xmlService" result="onResult();" method="POST" resultFormat="e4x" url="http://localhost:8084/TalkToFlex/TalkXML">
<mx:fault>Alert.show(event.toString(), event.type);</mx:fault>
<mx:request xmlns="">
<message>My new message</message>
</mx:request>
</mx:HTTPService>
<mx:Panel width="350" height="280" layout="absolute" horizontalCenter="0" verticalCenter="0" color="#000000" fontSize="12" fontFamily="Verdana">
<mx:Button x="126.5" y="10" label="Test Me" click="xmlService.send();"/>
<mx:TextArea x="10" y="42" width="310" height="188" id="txt" verticalScrollPolicy="on"/>
</mx:Panel>
</mx:Application>
Using the HTTPService class makes this a snap. Modify the URL to suite your needs and configuration. Run the project and you should see this:

