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.
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:
Before calling methods of a Java class or JavaBean from ActionScript, you must get a reference to the Java object.
#include "NetServices.as"
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.
gatewayConnection = NetServices.createGatewayConnection();
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.
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".
The following sections show the three pieces required to call a JavaBean from a Flash application that uses Flash Remoting MX:
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;
}
}
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:
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;
}
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
Comments
lc_kman said on Dec 22, 2003 at 8:23 AM : No screen name said on Feb 5, 2004 at 8:03 AM : Macromedia Flash Writer said on Feb 9, 2004 at 5:06 PM : No screen name said on May 7, 2004 at 7:50 PM : jfacurie said on Jul 19, 2004 at 2:10 PM : zebastien said on Aug 23, 2004 at 1:44 PM : patsch said on Nov 10, 2004 at 4:33 PM : tjsr said on Jan 17, 2007 at 1:40 PM :