Accessibility
Adobe
Sign in Privacy My Adobe

Flash Remoting TechNote

How to connect to a JavaBean using Flash Remoting and JRun 4/J2EE

This TechNote provides information on how to connect a simple "Hello World" JavaBean to Flash Remoting using JRun 4/J2EE. This TechNote is designed for users who are new to developing with Macromedia Flash MX and/or JRun 4/J2EE.

This tutorial will walk through the steps to connect your Flash movie to a JavaBean using Flash Remoting. The instructions will cover the following tasks:

Download and install the Hello World JavaBean sample files to follow along with this tutorial:

  1. Download the JavaBean-HelloWorld.zip file. Unzip the files using WinZip.
  2. Save the HelloWorld.jar file into your SERVER-INF/classes folder.
  3. Save the JavaBean-HelloWorld.fla file into your application. Then, open the FLA file using Macromedia Flash MX.
  4. The gateway URL is set to 8101. Edit the port number to match the port your server is currently running on.

How to create the JavaBean, compile it and place it in the correct location

  1. First, create a JavaBean that you plan to connect to Flash Remoting. For this example we will use the following HelloWorld JavaBean:
    public class HelloWorld
    {
    
    private String message;
    
    public HelloWorld() {
    message = "Hello World From JavaBean";
    }
    
    public void setMessage(String message) {
    this.message = "Hi " + message;
    }
    
    public String getMessage() {
    return message;
    }
    }
  2. Compile the JavaBean.
  3. Next, we will need to deploy this JavaBean in a location that will be accessible by the Flash Remoting gateway. Since the gateway is not local to your application, you will need to put the class file in one of the following locations:
    {MyServer}/SERVER-INF/classes This location exposes classes to the current server.
    {MyServer}/SERVER-INF/lib This location exposes JARs to the current server.
    {JRun.Install}/servers/lib This location exposes classes and JAR files to all server(s) running on JRun.
    Note: If you were to place the JavaBean in your WEB-INF/classes folder it would not be accessible by the gateway—unless you unzipped the flashgateway.ear file and deployed the gateway in your application.
  4. Make sure the folder you selected is in the classpath, by doing the following:
    1. Open JRun JMC.
    2. Click the "+" button to expand the server you are using.
    3. Select JVM Settings.
    4. In the Classpaths for VM, look for the folder that contains the HelloWorld.class file. For this example, we put the HelloWorld.class file in the SERVER-INF/classes folder.

      Note: If you don't see the folder in the classpath, then it must be added before continuing. After you have added the folder to the classpath, restart the server.

To recap this process, the steps above have illustrated how to create the JavaBean, deploy it to a directory accessible by the Flash Remoting gateway, and verify that the JavaBean is in the classpath. Next, we'll discuss how to build the front-end.

How to build the Macromedia Flash Movie

  1. Open Macromedia Flash MX and draw a box, using the Rectangle tool from the toolbox.
  2. Using the Text tool, draw a text box on the stage and type the following:

    Data from JavaBean
  3. Draw another text box on the stage. This time, do not type any text. This text box will be used to display the data from the JavaBean, as follows:
    1. Choose Window > Properties to launch the Properties inspector (if it is not already visible).
    2. Select "Dynamic Text" from the pop-up menu.
    3. In the field titled "Instance Name", enter:

      messageOutput

The Instance Name "messageOutput" is used when we reference the instance in the ActionScript to display the data returned by the JavaBean.

The illustration below provides a visual representation of how the movie might look at this point:

How to write the ActionScript

  1. Click the Actions window, or select Window > Actions.

    When you first launch the Actions window, the default setting is Normal Mode. To insert code while in Normal Mode, you must select the "+" sign and choose the desired functions from the list of Actions on the left side of the window. We will use Expert Mode instead—because this allows you to type the ActionScript directly into the Actions window. Select Expert Mode from the pop-up menu, as shown below:

  2. Select Frame 1 from the pop-up menu. First, make sure to include the NetService.as class file into the first frame:
    #include "NetServices.as"
    Note: The NetServices.as class is responsible for making the connection to the gateway. This step is mandatory.
  3. Next, we'll create the connection to the Flash Remoting gateway:
    // connect to the gateway server
    if (inited == null) {
    
    inited = true;
    NetServices.setDefaultGatewayUrl
     ("http://localhost:8101/flashservices/gateway");
    gatewayConnnection = NetServices.createGatewayConnection();
    flashtestService = gatewayConnnection.getService
     ("HelloWorld",this);
    }
    This ActionScript sets the default gateway URL, connects to the gateway and creates a Service Object—in this example, the Service Object is flashtestService.

    The methods in the JavaBean are exposed to Flash as Service Functions. The two methods in the HelloWorld JavaBean are setMessage and getMessage. Therefore, the two Service Functions available in the Service Object we created are:

    getMessage() and setMessage().
  4. Return the data from the JavaBean using the Service Function in Flash, as follows:
    // call the service function getMessage()
    flashtestService.getMessage();
    If you run the movie now (by selecting CTRL+ENTER) you will not see the data in Flash yet... but you will receive a message in the Output window, stating:

    NetServices info 1: getMessage_Result was received from server: Hello World From JavaBean

    This message indicates that the connection was successful. It also indicates that although you didn't call the getMessage_Result Service Function, it was returned to the Flash movie.
  5. Next, we need to add ActionScript to return the data and display it in the text box we created in the Flash movie. This code will accomplish that goal:
    // use _Result to have Flash call this function
    function getMessage_Result(result) {
    messageOutput.text = result;
    }
     
    // use _Status to handle any errors
    function getMessage_Status(result){
    messageOutput.text = "status: " + result.details;
    }
    When you use _Result after a Service Function, Flash will automatically call that function. In this case we are calling the getMessage Service Function. We can now reference the Instance Name messageOutput that we created in the Flash movie, which will display the data in the text box.

    When you use _Status after a Service Function, Flash will call this function if there is an error. In this example, the error message would be displayed in the text box we created.

How to play the Flash movie and use the NetConnection Debugger

  1. To run the movie select File > Publish Preview, (or CTRL + ENTER).

    You should see the "Hello World From JavaBean" data displayed in the Text box we created, as shown in the illustration below:
  2. Run the NetConnection Debugger to see how the debugger works. Take some time to review the data provided by the NetConnection Debugger.

    The first thing you need to do is include the NetDebug.as class. Add the NetDebug.as class just below the NetServices.as include, using this code:
    #include "NetDebug.as"
    1. Save the Flash movie.
    2. Open the NetConnection Debugger, (by selecting Window > NetConnection Debugger).
    3. To keep the NetConnection Debugger in focus, select CTRL+ENTER.
    When you run the movie with the NetConnection Debugger in focus, you can watch the Debugger print the output as it occurs.

Last updated: June 24, 2002
Created: May 24, 2002
ID: 16360
Product: Flash Remoting
Versions: All
OS: All
Browser: All
Server: None
Database: None
Former ID: N/A
 

Provide Feedback

less more

1

2

3

4

5

(300 characters or less - you will not receive a reply.)