Calling Java classes or JavaBeans from ActionScript

This section describes how to call a JavaBean or Java class from Flash using Flash Remoting MX. There is only one significant difference in how Flash Remoting MX handles standard Java classes and JavaBeans. A Java class is stateless and a new object instance is created whenever a method is invoked. A JavaBean is stateful in the user's HTTP session. Using a JavaBean with Flash Remoting MX is similar to using the jsp:useBean tag in a JSP. Flash Remoting MX sends a JSESSIONID parameter to the Flash application, and NetServices appends the session ID value to all subsequent HTTP requests.

Note:   The Flash Remoting session is independent of HTTPSession objects available to JSPs and servlets. A stateful JavaBean instantiated through Flash Remoting MX cannot access an object stored in a session by a JSP or servlet. Conversely, a JSP or servlet cannot use its session to access a JavaBean instantiated through Flash Remoting MX.

Making a Java class or JavaBean available to Flash Remoting MX

To call a standard Java class or JavaBean with Flash Remoting MX, the class or bean must be available in the classpath of the Flash Remoting gateway. Unless the class or bean is in the same web application as the gateway, you typically add it to the system classpath.

The following table lists standard ways to add classes to the system classpath:
Application server
Classpath information
Sun ONE
Web Server
In the Web Server Administration Server console, add classes to the Classpath field in the Configure JVM Attributes page of the Java panel.
WebSphere
In the WebSphere Application Server Console, add classes to the Classpath field of the JVM Settings page for your server. In the server-cfg.xml tree in the left pane of the console, JVM Settings is under WebSphere Administrative Domain > Nodes > nodename > Application Servers > servername > Process Definition.

Note:   To call a class or JavaBean in WebSphere, you also must grant clients permission to access the package that contains the class or JavaBean. To do this, you add a line to the default permissions granted to all domains in the websphere_root/AppServer/java/jre/lib/security/java.policy file. For example, the following line lets users access the Flash Remoting sample classes in the flashgateway.samples package:

permission java.lang.RuntimePermission "accessClassInPackage.flashgateway.samples";
JRun
Copy classes to the jrun_root/jrun_server/SERVER-INF/classes directory in subdirectories that match the package structure of the classes. Copy JAR files to the jrun_root/jrun_server/SERVER-INF/lib directory.

Getting a reference to a Java class or JavaBean in ActionScript

Before calling methods of a Java class or JavaBean from ActionScript, you must get a reference to the Java object.

To get a reference to a Java object:

  1. Include the NetServices.as file:
    #include "NetServices.as"
    
  2. Specify the default Flash Remoting gateway URL:
    NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway"); 
    

    Note:   There are several other ways to specify the gateway URL. For more information, see "Configuring Flash Remoting MX," in Chapter 2.

  3. Connect to the Flash Remoting gateway:
    gatewayConnection = NetServices.createGatewayConnection();
    
  4. Get a reference to the Java class or JavaBean in the HTTP session, as shown in the following example:
    flashtestService = gatewayConnection.getService
    ("flashgateway.samples.FlashJavaBean", this);
    

    The first parameter of the getService function is the fully qualified class name of the Java class or JavaBean. The second parameter of the getService function, this, specifies that the results of service function calls are returned to this Flash timeline.

Invoking Java methods in ActionScript

Once you have a reference to a Java class or JavaBean, you can use ActionScript functions to invoke that object's public methods. For example, to invoke the following JavaBean method:

public String getMessage() {
        count++;
        return message + " (count=" + count + ")";
    }

You could use the following ActionScript code, assuming flashtestService represents your reference to the JavaBean:

function getMessage()
{
  flashtestService.getMessage();
}

To handle the function results, you use a result handler function like the following:

function getMessage_Result( result )
{
  messageOutput.text = result;
}

For more information about handling function results in ActionScript, see "Handling function results in ActionScript".

Looking at a Flash application that calls a JavaBean

The following sections show the three pieces required to call a JavaBean from a Flash application that uses Flash Remoting MX:

Looking at the JavaBean

The example Flash application invokes the setMessage, getMessage, testBoolean, and testDate methods of the following JavaBean:

package com.samples;
import java.util.Date;
import java.io.Serializable;
import org.w3c.dom.Document;

public class FlashJavaBean
        implements Serializable {

    private String message;
    private int count;

    public FlashJavaBean() {
        message = "Hello World From JavaBean";
        count = 0;
    }
    public boolean testBoolean(boolean b) {
  return b;
    }
    public Date testDate(Date d) {
  return d;
    }
    public void setMessage(String message) {
        this.message = "Hi " + message;
    }
    public String getMessage() {
        count++;
        return message + " (count=" + count + ")";
    }
    public int getCount() {
        count++;
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public Document testDocument(Document doc) {
  return doc;
    }
}

Looking at the user interface

The following figure shows the user interface of the example Flash application with callouts that indicate the field types and variable names referenced in the ActionScript code:

Looking at the ActionScript

The following code shows the ActionScript of the example Flash application, with comments in bold:

// Include NetServices library.
#include "NetServices.as"
#include "NetDebug.as"
if (inited == null)
{  
  // do this code only once
  inited = true;

// Set the default gateway URL.
  NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway");

// Connect to the gateway.
    gatewayConnection = NetServices.createGatewayConnection();
// Get reference to JavaBean:
    flashtestService = gatewayConnection.getService
    ("flashgateway.samples.FlashJavaBean", this);
  
  flashDate = new Date();

// Set initial text for messageInput and dateInput text fields.
  messageInput.text = "[Enter a Message]";
  dateInput.text = "" + flashDate;
}

// Invoke business methods when user clicks the runButton.
function runExample()
{
  setMessage();
  getMessage();
  testBoolean();
  testDate();
}

// Business functions.
function setMessage()
{
  flashtestService.setMessage(messageInput.text);
}

function getMessage()
{
  flashtestService.getMessage();
}

function testBoolean()
{
  if (trueRadio.GetState())
  {
    flashtestService.testBoolean(true);
  }
  else
  {
    flashtestService.testBoolean(false);
  }    
}

function testDate()
{
  flashDate = new Date();
  dateInput.text = "" + flashDate;
  flashtestService.testDate(flashDate);
}

// Handle results from server; display results in output fields.
function setMessage_Result( result )
{
}

function getMessage_Result( result )
{
  messageOutput.text = result;
}

function setMessage_Status( result )
{
  messageOutput.text = result.details;
}

function getMessage_Status( result )
{
  messageOutput.text = result.details;
}

function testBoolean_Result(result)
{
  boolOutput.text = "result: " + result;
}

function testBoolean_Status(result)
{
  boolOutput.text = "status: " + result.details;
}

function testDate_Result(result)
{
  flashDate = result;
  dateOutput.text = " " + flashDate;
}

function testDate_Status(result)
{
  dateOutput.text = "Status: " + result.details;
}