DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 20.13 Calling Java or JSP Functions from Flash

20.13.1 Problem

You want to invoke J2EE service functions using Flash Remoting.

20.13.2 Solution

Use public Java class methods. Alternatively, use servlets/JSPs that have been modified to work with Flash Remoting.

20.13.3 Discussion

Although Flash Remoting for J2EE enables you to invoke public methods of Java classes or servlets/JSPs, invoking Jave class methods is the preferable option. You can invoke any public method of a Java class without having to modify the Java code at all. This is an obvious advantage. To access the methods of a Java class from Flash Remoting, you should create an ActionScript service object that maps to the fully qualified Java class name:

// Create a service that maps to the Book class in the oreilly.ascb package.
myService = myConnection.getService("oreilly.ascb.Book");

If you must use servlets/JSPs instead of invoking Java class methods, you need to modify the servlet/JSP code for it to work properly with Flash Remoting. Values are passed to and from the servlet/JSP by way of attributes of the request object. Parameters are passed from Flash to the servlet/JSP using the attribute named FLASH.PARAMS. You can retrieve the attribute value using the getAttribute( ) method of the request object. For example:

Object myFlashParams = request.getAttribute("FLASH.PARAMS");

The value of the FLASH.PARAMS attribute is a List object. However, if you want the servlet/JSP to be accessible from a web browser as well as from Flash Remoting, you should cast the value only after checking to make sure it actually is an instance of List. Otherwise, when the servlet/JSP is accessed from a web browser (and no FLASH.PARAMS attribute exists), the attempt to cast the object to List will cause an error. Therefore, you should use:

if (myFlashParams instanceof List) {
  List myParamsList = (List) myFlashParams;
  // Process the parameters here.
}

You can return a value to a Flash movie from a servlet/JSP using the FLASH.RESULT attribute of the request object. Assign the value you want to return to Flash to the attribute using the setAttribute( ) method. The setAttribute( ) method accepts complex datatypes only. You cannot set the attribute value to a primitive datatype such as float. If you want to return a primitive datatype, use a wrapper class instead, such as:

// Return a floating-point number to Flash using the Float class.
request.setAttribute("FLASH.RESULT", new Float(6.6));
    [ Team LiB ] Previous Section Next Section