View comments | RSS feed

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;
}

Comments


lc_kman said on Dec 22, 2003 at 8:23 AM :
There appears to be an error on this page. The Java class is in the package "com.samples" and the actionscript calls the Java file as "flashgateway.samples". Of course, I am having some trouble getting my own java files to work so mabey you are supposed to use two different package names when calling these objects. They do not give an explination as to how the two different package names find each other or interact. Something to look at, and keep in mind.
No screen name said on Feb 5, 2004 at 8:03 AM :
In one way, you have certainly managed to beat Microsoft...

in confusing any issue, never be specific about something.
Can you ever handle and explain one problem and supply a working solution.??

All one is getting all the time is about 50 times in one article the phrase:
For more information about blablabla refer to <http:\\blablabla\blablabla>, loosing everybody's interest and focus.

Can you ever write about an issue, suppling the necessary background and examples, in a way the reader can understand
and work trough it, without referring to 10000 other articles who suppose to have a solution.

Your tutorials are grap, a peace of "global" rubbish and statements, with no examples or background information.
Not even a API for your actionscript is available. All a big secret...
Coding in any OOP language, I must be able to refer to interfaces and implementations. constructor functions or methods.

To cut it short.
Looking for something useful, solving even the smallest problem, is on a MM Site simply a waste of time.
You end up with 50 open windows on your desktop and you seem to know afterwards less then ever before.

Even your products are excellent, why do you leave it up to strangers to write MM understandable manuals like
the ASDG Reference guide from Moock?

Ps: it took me 3 days to figure out what exactly an flashgateway is. (communicating with a Java servlet on a Tomcat server).


and you know ...I gave it up. unusually



Best regards


Rainer
Macromedia Flash Writer said on Feb 9, 2004 at 5:06 PM :
We are making efforts to improve the quality of our documention and will contact you directly to see what we can do to better address your concerns.
No screen name said on May 7, 2004 at 7:50 PM :
I somewhat agree with the previous comment. I am going around in circles trying to get a simple hello world working on tomcat. It would be nice to have a complete working example for this very simple environment (tomcat)
jfacurie said on Jul 19, 2004 at 2:10 PM :
I'd also be interested in having a procedure for installing Flash Remoting MX within Tomcat.

Thanks
zebastien said on Aug 23, 2004 at 1:44 PM :
Hello, A comment on the following: "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."
I'm still unclear if there is a way or not to transfer the session information from a JSP to a flash movie; if not, how to accomplish SingleSignOn supported per most J2EE app srvs now ? what about JAAS support ? what about session tracking ?
currently if I try to launch a flash movie from a servlet, all of that is disabled because the flash client creates a new session.
Any hints ?
Sebastien.
patsch said on Nov 10, 2004 at 4:33 PM :
We use Cookies as an alternative to pass state information between JSP's and Flash - login occurs through a JSP, which then sets a cookie that the flash app can read. If anyone has found a better solution to this problem I'd be interested to hear about it.
tjsr said on Jan 17, 2007 at 1:40 PM :
The hard part about transferring data between a flash remoting connection/session and a Java/JSP session is identifying that they are the same session.
If you can identify this, you can then just store the information in a static HashMap or something similar somewhere in one of your classes.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/usingFRJ2EE3.htm