Christian Cantrell
Macromedia
Macromedia Flash Remoting Makes Macromedia Flash an Alternative to JSP and Applets
Traditional User Interface Options
When developing a web-based application in Java, developers have two options for presentation technology: JSP and Java applets. JSP is the Java version of a relatively standard web scripting technology, analogous to ASP, PHP, and ColdFusion. Applets are truly something unique in that they allow applications to run partially on the client's machine and partially on the server.
The problem with JSP is that out of the box, you only have a handful of tags, several of which you never end up using, so you either end up with a lot of scripting in your pages, or you spend a great deal of time building, testing, and maintaining an extensive set of custom tags before you ever make any real progress on your applications. Comprehensive tag library projects, such as the Java Server Pages Standard Tag Library (or JSTL) now address this problem; developers can perform common web-application tasks, like using conditionals, iterating through data structures, XML parsing, text formatting, and database access. Tag libraries like JSTL bring JSP closer to the level of functionality of languages, like ColdFusion, PHP, and ASP, but even with every single tag you could possible want, web scripting languages limit you to only generating HTML. Applications based on HTML are perfect for very content-rich sites, since HTML is still the best way to present documents over the web; however, if your application requires anything more than basic functionality, HTML limits you.
That's where applets provide a clear advantage over not just JSP, but HTML-based applications in general. The Java Advanced Windowing Toolkit and Swing give developers access to a comprehensive set of GUI widgets that they can use to build more intuitive and interactive interfaces than are generally possible with the extremely limited set of form inputs available in HTML. In theory, it's great, but in practice, applets never made the tremendous impact most of us thought they would. They were never widely adopted due to browser and plug-in incompatibilities and generally poor performance. Additionally, building user interfaces in Java is not nearly as simple and straightforward as building them in HTML; it is not nearly as easy to pass a Java applet to a designer to "skin" or clean up as you can with an HTML-based application. Java applets are so seldom used now that I can't even remember the last time I came across a site with a Java applet imbedded in it.
So how can Java developers get the convenience of an extensive tag library and the rich functionality of an applet in a single, widely supported technology? Thanks to Macromedia Flash Remoting, the best option available today is Macromedia Flash MX.
What is Flash Remoting?
Flash Remoting and Macromedia Flash are two distinctly different things. Macromedia Flash is a vector-based technology that creates user-interface components, design elements, and sophisticated animations and displays them in either a standalone Macromedia Flash player or more often, through your browser, embedded in an HTML page. Flash Remoting is the best way for Macromedia Flash applications to exchange data between a client and server. Imagine a travel agency embedding a Macromedia Flash application in its website to make searching for fares faster and more convenient for their customers. The user chooses a set of criteria through a Macromedia Flash interface with a number of sophisticated GUI widgets and then clicks a "Find Lowers Fares" button. The Flash Remoting technology enables the Macromedia Flash application to serialize the user entered data and send it to the server; the server processes the data and retrieves an appropriate response; Flash Remoting returns the result set to the client, where the application uses it to update the interface for the user.
You can think of Flash Remoting as RMI (Remote Method Invocation) for Macromedia Flash, or quite simply, the glue between the client and the server.
The Advantages of Macromedia Flash
There are five primary advantages to implementing an application presentation layer in Macromedia Flash:
- Macromedia Flash provides a powerful, intuitive drag-and-drop graphical authoring tool that is comfortable for both designers and developers. Designers are comfortable using the Macromedia Flash authoring tool design capabilities while developers can use to drag-and-drop interface components and ActionScript, the Macromedia Flash object-oriented scripting language.
- The industry contains an abundance of Macromedia Flash expertise. There are currently over one million web developers using Macromedia Flash to enhance user experiences. Admittedly, not all of them write applications in Macromedia Flash, however Flash Remoting simplifies server integration; adding a server component to any piece of Macromedia Flash makes it more interactive and data aware. It only takes a few lines of code.
- Macromedia Flash is platform independent (like Java) and widely supported. To use Flash Remoting, the client must have the Macromedia Flash 6 player installed. Currently, 98.4% of Internet users have the Macromedia Flash player installed, and there are Macromedia Flash 6 players available for Windows, Linux, Macintosh, and even PocketPC operating systems.
- Macromedia Flash allows your applications to have quick-loading, dynamic interfaces. Since Macromedia Flash is vector-based, Macromedia Flash binaries (SWF files) are very small and quick to load across even the slowest Internet connections. Additionally, Macromedia Flash supports dynamic loading of additional resources like images, sounds, video, and additional pieces of Macromedia Flash, so your application can load assets on demand.
- Macromedia Flash and Flash Remoting allow applications to restrict network traffic only to essential data between the client and the server. Since the client's browser only downloads the Macromedia Flash application once, this process restricts additional network traffic to data exchange instead of each response having to contain several kilobytes of presentation information, which is the case with JSP and other HTML-based scripting technologies.
Although Macromedia Flash is extremely versatile and supports a wide range of media types and functionality, it is not the right choice for all web-based applications. Macromedia Flash excels at enabling developers to providing a rich, intuitive, interactive experience to their users; however, for applications that primarily present large amounts of textual information, HTML is still currently your best bet.
What Can Macromedia Flash Do?
Macromedia Flash started its life as a vector-based animation tool designed to give web developers the ability to embed fluid and compact animations in their web pages. That was a long time ago, however, and Macromedia Flash has changed immensely. Starting with Macromedia Flash 4, people began building user interfaces out of Macromedia Flash because of its platform independence, scripting support, and intuitive authoring environment. The most current version of Macromedia Flash, Macromedia Flash MX (analogous to version 6), is as different from Macromedia Flash 4 as Macromedia Flash 4 was from Macromedia Flash 1. Macromedia Flash MX is now a full-fledged application development environment which supports:
- XML
- Socket connections
- Object-oriented ECMA compliant scripting language (ActionScript)
- Component architecture for GUI elements
- Styles to control look and feel
- Event broadcasting
- Object serialization
- Local data storage
- Remote method invocation through Flash Remoting
Macromedia Flash MX exposes all of this functionality by combining an intuitive drag-and-drop authoring tool and a powerful with a well encapsulated ActionScript API. The Macromedia Flash authoring tool saves applications as FLA files, which it compiles into SWF files before streaming them to client browsers.
Thin Clients and Rich Clients
Does building your application's presentation layer in Macromedia Flash mean giving up on the concept of a thin client? Not at all. Macromedia Flash clients can be as thin, thick, or as rich as your architecture dictates. A Macromedia Flash application can contain nothing more than form elements, data tables, and client-side validation, leaving all the application's business logic safely encapsulated on the server.
In fact, most multi-tiered application architectures can accommodate an entirely new, rich Macromedia Flash presentation layer without re-factoring the business layer (this includes EJB-based architectures). On the other extreme, I have seen successful applications where all the presentation and business logic were implemented in Macromedia Flash leaving the server to do nothing more than data persistence. Just like JSP and Java applets, Macromedia Flash and Flash Remoting let you decide how to structure your application.
A Closer Look at Flash Remoting
Flash Remoting is the glue between the client and server. It provides developers with a sophisticated but very straightforward API for invoking remote functions and passing serialized objects back and forth between the client and the server. It uses a protocol called AMF (Action Message Format) to exchange data with the server, which is a compact, binary data format. Flash Remoting contains both a client-side element which is installed into the Flash authoring environment, and a server-side element called the Flash Remoting adapter. There are currently Flash Remoting adapters available for ColdFusion, .NET, and, of course, Java (ColdFusion and JRun servers come with Flash Remoting adapters already integrated).
Since Macromedia Flash MX has native support for XML, developers sometimes ask me what advantages Flash Remoting has over SOAP or XML-RPC. While I would not discourage using SOAP or XML-RPC with Macromedia Flash, I will say that Flash Remoting has an advantage over XML in the level of encapsulation it provides. Flash Remoting handles all object serialization completely seamlessly, so it is as easy as configuring a connection between the client and the server, invoking remote functions, and handling the result in your client-side call-back functions.
Using Flash Remoting to Integrate Macromedia Flash with Java
To invoke remote Java services from Macromedia Flash, install Flash Remoting on both the client and server. Installing Flash Remoting on the client is simple. Just download the free Flash Remoting components and double click the installer (available for both Windows and Mac). The Flash Remoting Components installer will install all the ActionScript APIs you need on the client and the NetConnection Debugger, which is a utility you use to snoop on Flash Remoting network traffic between your client and server.
Next, download the Flash Remoting adapter for your server. The Java Flash Remoting adapter is implemented as a servlet and will therefore work with any servlet engine. Follow the appropriate installation process based on the servlet engine you are using.
Once you have both your client and your server configured, invoking a remote Java service is as simple as:
- Configuring a connection between the client and server.
- Getting a remote reference to class that contains the method you want to invoke.
- Specifying an ActionScript object which implements callback functions.
- Invoking the remote method and handing the result in the object that contains the callback functions.
Flash Remoting will let you invoke JavaBeans, Java classes, EJBs, servlets, JSPs and JMX Mbeans (JRun only). The data types that you can exchange between Macromedia Flash and Java are RecordSets, Objects, primitives, Strings, XML, EJB references, and Date objects. Flash Remoting will even maintain a session between the client and server by exchanging a jsessionid parameter.
Summary
Macromedia Flash and Flash Remoting give Java developers the ability to build richer, more interactive, more efficient, and more intuitive user interfaces than are possible with JSP and Java applets. The Macromedia Flash authoring environment provides both designers and developers with the tools and functionality they need to deliver next-generation Rich Internet Applications to clients, and truly unique Internet experiences to end users.
For more information on Flash Remoting and how to get started, see the following articles and tutorials:
- Flash Remoting 101 for Java Users
- Macromedia Flash MX Evaluation Resource Center
- Designing Java Applications for Flash Remoting
- Consuming Web Services with Macromedia JRun 4 and Flash Remoting
- Flash Remoting for J2EE Developers
About the author
Christian Cantrell is the Macromedia Server Community Manager. He has been developing large-scale, web-based applications in ColdFusion, Java, JSP, and Macromedia Flash for the last five years. He is the author of numerous tutorials and whitepapers, and is coauthor of Flash Enabled: Flash Design & Development for Devices. Keep up with Christian by reading his blog.
Submit feedback on our tutorials, articles, and sample applications.