Guy Rish

Subscribe to Guy Rish: eMailAlertsEmail Alerts
Get Guy Rish via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: ColdFusion on Ulitzer, Java Developer Magazine

CFDJ: Article

A Cold Cup O'Joe Part 8 of 8

A Cold Cup O'Joe Part 8 of 8

Java applets are a much-maligned technology in the Internet world. Originally a major focus of the Java platform, they've retreated from their former place in the spotlight.

Problems with browsers, corporate conflicts, and being outflanked by faster and (arguably) lighter-weight tools like Macromedia's Flash have diminished the popularity of applets considerably. Despite this, they continue to be used for a variety of purposes. What's more, they're often the only solution since most other technologies have focused on limited platforms while Java purports to be usable everywhere.

For several versions of ColdFusion, applets have enhanced the client side of a number of ColdFusion applications. While CFML has a tag, CFAPPLET, for integrating with applets, some applets have actually prompted the creation of specialized tags just for their functionality. Tags like CFGRID and CFTREE have been in the vocabulary of ColdFusion developers for some years now.

Like Java servlets, and unlike the other Java technologies discussed in this series, no special configuration needs to be made in the Java Settings page for these to work. Everything happens in the browser. The only time the ColdFusion Administrator is needed is to register an applet for use with the CFAPPLET tag.

In this article all examples assume that the accompanying files are installed in a directory called "banners" off the local Web server's root directory.

The Applet
This article shows a number of different ways to integrate Java applets in a ColdFusion application. To stay within the scope of the article, I'll use a simple but useful banner applet to display a single clickable image or rotate through a set of clickable images.

For convenience, the applet, ClickBanner, is contained in a single Java source (producing two class files because of an inner class). The class runs an additional thread if more than one banner item is specified. ClickBanner takes a number of parameters, as seen in Table 1.

Applets in HTML
There are two tags in HTML for handling Java applets: the deprecated APPLET tag and the newer OBJECT tag. Despite being deprecated, the APPLET tag is still broadly used. In fact, both Sun's latest applet examples and ColdFusion's own CFAPPLET tag (discussed later) use it.

The original embedded-content tag, APPLET is pretty simple to use, as can be seen in the snippet below (from Listing 1, AppletExample1.html):

<applet code="ClickBanner.class"
codebase="http://localhost/banners/" width="468" height="60">
<param name="item0"

There are actually a broader number of attributes for this tag, as shown in Table 2. (This list doesn't include the attributes added to all HTML elements in 4.x for consistency, such as style, alt.)

One problem with using the OBJECT tag is that it necessitates use of the Java plug-in. Although a discussion on the use of the OBJECT tag with Java applets is beyond the scope of this article, extensive documentation is available at http://java.sun.com/products/plugin/. There are even tools for automatically migrating your APPLET tag directives to OBJECT tag directives.

Listing 2 is an HTML document, AppletExample2. html, that contains an example, but it works only if the Java plug-in is installed.

Applets in ColdFusion
Using ColdFusion's applet interface, CFAPPLET, is an excellent way to work with applets that have a fixed set of parameters. There are a couple of prerequisites:

  • Applets must be registered in the ColdFusion Administrator.
  • CFAPPLET must be used subordinate to CFFORM.
CF Administrator's Java Applets Page
Using an applet with the CFAPPLET tag requires that it first be registered through the ColdFusion Administrator. The main Java applets page (see Figure 1) can be seen on the Administrator's default server menu display under the Extensions heading (Server > Extensions > Java Applets).

Depending on your configuration (and whether you're using ColdFusion 4.5x or 5.x), you may see other applets already registered. The most common one is the COPYTEXT applet entry (used with ColdFusion's own applet-backed tags). It's here that you register the ClickBanner applet:

  1. Click the "Register New Applet" button.
  2. Complete the Add/Registered Applet page (shown in Figure 2) and submit the form with the values in Table 3.
The CFML wrapper on the APPLET tag, CFAPPLET, requires preparation in the CF Administrator and must be placed subordinate to a CFFORM tag. Aside from those basic requirements, using the CFAPPLET tag has only two required attributes (the complete list appears in Table 4): appletSource, the name of the registered applet; and name, the identifier given to the applet instance. This is mostly because so many defaults can be gotten from the registered data supplied to the Administrator.

Since the majority of the settings are looked up in the CF server registry, calling a registered applet with CFAPPLET is short work, as exemplified in this snippet from AppletExample3.cfm (see Listing 3):

<cfform action="http://localhost/banners/AppletExample3.cfm" method="POST">
<cfapplet name="banner" appletsource="banner"

These few lines give us everything we've come to expect from the previous examples.

Applet-Backed Custom Tag
The two approaches looked at so far place the developer fairly close to the applet itself. HTML's APPLET tag and and CFML's CFAPPLET tag require you to know a thing or two about the applet being used. This is where wrapping the applet in a custom tag can be very powerful. (This was actually done early on for CFML additions with CFGRID, CFTREE, and the like. These UI-oriented tags are really just thin wrapping over some pretty robust Java applets.) Doing so provides a number of important benefits: defaulting content, derived and housekeeping attributes, and applet lifecycle shielding.

Defaulting Content
While CFAPPLET allows for this through the applet registry, it doesn't allow you to script the logic. Using a custom tag allows a reactive default to be set where conditions can be checked.

Derived and Housekeeping Attributes
Certain things that can foul up a page through a minor typo can often be avoided this way. Specifically, in the case of the ClickBanner applet, where a count parameter is set to tell the applet how many image/link banner combinations to pick up, this is a very useful feature. The ClickBanner custom tag counts these and generates the count parameter itself.

Applet Lifecycle Shielding
Once an interface has been established via a custom tag, minor changes to the applet can be hidden. Applet parameter name changes, changes to default values, even new required parameters can often be masked by a custom tag.

ClickBanner Custom Tag
Following in the footsteps of ColdFusion's own designers, I've created a custom tag to wrap the ClickBanner applet. This custom tag is really a set with one subordinate to the other. CF_CLICKBANNER is the parent tag and sets up the applet; CF_BANNERITEM is the subordinate tag identifying the individual banner images and links. It won't take long to see that these two tags simplify the coding. CF_CLICKBANNER has only two required attributes (see Table 5 for the complete list), as does CF_BANNERITEM (see Table 6).

A simple example of this tag set can be seen in the following snippet (from Listing 4, AppletExample4. cfm):

<cf_clickbanner width="468" height="60">
<cf_banneritem image="http://localhost/banners/megatokyo.gif"
Wrapping It Up
Despite the maligning and declining popularity, Java applets still have numerous uses. They can be very useful when concerns about the target platform would make alternate solutions difficult to implement. While some tools are becoming more technologically competitive, their penetration is not nearly as great (Java applets run everywhere Flash does, but Flash doesn't run everywhere an applet does).

Given the variety of different implementations available in CFML for incorporating an applet, they can in many cases be considered a slam-dunk option. Like any technology, when used judiciously it can be a great benefit.

More Stories By Guy Rish

Guy Rish is a ColdFusion and .NET developer at Vente as well as President at Gestaltech. He is an active developer and writer for various languages and technologies, and has contributed work in books on ColdFusion MX, Flash MX, and Dreamweaver MX. He blogs at ccoj.coldfusionjournal.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.