Using BlazeDS to Enable Flex to Send Data To A Java Class


This blog entry continues my exploration of how to use Flex, Java, and BlazeDS together. In this tutorial I explain how to send data from Flex to a Java class.

Setup This Example

Now that I've got BlazeDS providing the plumbing on my server so the my Flex application can communicate with the backend Java classes, it's time to start testing how to use Java on the backend instead of my previous (and beloved) ColdFusion 8. If you've not yet read the first tutorial on setting up BlazeDS with Flex and Java, then check it out before trying this example.

This example will continue the simple HelloWorld from the previous tutorial. The goal with this iteration is to send a String value from Flex to the Java class method that will cause the method to return a custom String message.

You can view a demo of this example here.

Here is the Java class:

package example;

public class HelloWorldName {
    public HelloWorldName() {
    public String getHelloByName(String aName){
        return "Hello from Java, " + aName + "!";


Note that the getHelloByName method must receive a String value for its parameter. Compile this Java class and place the resulting class file in [tomcat-home]\webapps\blazeds\WEB-INF\classes\example (since the class package statement is package example, the compiled class should go in the example folder under classes).

Add the following destination node to the remote-config.xml file.

<destination id="helloworldname">
<adapter ref="java-object"/>

Stop Tomcat if its running and then restart Tomcat.

Create a new Flex project following the guidelines given in the first tutorial. Below is the code that goes between the mx:Application tags.


import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.utils.StringUtil;

Called when sayHelloBtn is clicked

private function processSayHello():void {

ro.getHelloByName( names.value );

}//end function processLogin

private function resultHandler(event:ResultEvent):void

    //used for debugging - shows details about result
//returned by the Java class method ObjectUtil.toString(event.result) );


private function faultHandler(event:FaultEvent):void
{ ObjectUtil.toString(event.fault) );


<!--note the compiled java class HelloWorldName should be in a folder
named example and under the blazeds\WEB-INF\classes
folder which is in webapps-->

<mx:RemoteObject id="ro" destination="helloworldname"

<mx:Panel x="10" y="10" width="440" height="200" layout="vertical" title="Test Flex 3 Using Java" borderColor="#008040" fontFamily="Arial" fontWeight="bold" fontSize="13">
<mx:Text text="Select a name to say hello to and then click the button" fontWeight="bold"/>
<mx:Spacer height="10"/>

<mx:ComboBox id="names" width="250">

<mx:Spacer height="10"/>

<mx:Button label="Say Hello!" id="sayHelloBtn" click="processSayHello()"/>


When the user clicks on the button, ActionScript function processSayHello is called. In this function the program tells the ro (id value for the mx:RemoteObject) to call the getHelloByName method and sends that method the String value of the selected name in the names ComboBox.

If the result from the calling the Java class is returned successfully, then ActionScript method resultHandler() is called. An alert box is displayed which contains the String returned by the HelloWorldName Java class method getHelloByName.

Important Points

Note one thing about the mx:RemoteObject tag. This time I did not include the source attribute. From what I've read about BlazeDS, the source attribute is not necessary in the mx:RemoteObject tag if the destination value has been specified in the Remote-config.xml file. If you examine the remote-config.xml file addition (see above) I specified a destination node with an id value of helloworldname. This node contains a child node of source that provides the name of the Java class file to call upon.

One other thing to note about the Java class file. According to the BlazeDS documentation, the class file must have a default constructor (a constructor with no parameters).

With BlazeDS providing the means to integrate Flex and Java, using mx:RemoteObject to communicate with a Java class seems to be just like how I used mx:RemoteObject to communicate with ColdFusion Components (CFCs). I hope that will continue to be true as I explore more complicated Flex-Java integration examples.

What's Next

Next I'll work up an example of mapping an ActionScript 3.0 class to a Java class so that Flex and Java can exchange user-defined data types (such as a Person object or a collection of Person objects).

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
hi Bruce,
Really awesome! This article helped me in making BlazeDS more clear.
Rahul Sahay
# Posted By Rahul Sahay | 6/26/08 3:54 AM
Hi Bruce,
A really goood tutorial. Was stuck how to use java asa Back end for Flex 3. thanks.
I have FDS on my tomcat, will it work if i use the code given by you?
And also, how is navigation possible in Flex 3. For eg. after a user logs in, how do u redirect him to his home page???

Thank you
# Posted By Khadija | 8/12/08 11:45 PM

I've got a problem when a click over Say Hello! My web browser gets Frezee..."Transfering data from localhost" and there's not Alert on Screen saying "Hello Bruce". The first example worked good. This one no. Do you know something about this?

Thanks in advance
# Posted By matias | 9/2/08 11:06 PM
Ok, it's done. The problem is gone!
# Posted By matias | 9/3/08 3:58 PM
I have a bean say Person Class at Java side. and now I have to create a new person at the flex side and send it to java side to insert it into the data base.
How do I do that.
I have create a action script whcih matchs to the person class and tried it but getting and exception saying cannot convert to required example.Person class

Can you pls help.
Thank oyu
# Posted By Manish | 7/23/09 5:46 PM
BlogCFC was created by Raymond Camden. This blog is running version Contact Blog Owner