Embedding Java applets



The cfapplet tag lets you embed Java applets either on a ColdFusion page or in a cfform tag. To use the cfapplet tag, first register your Java applet using the ColdFusion Administrator Java Applets page (under Extensions). In the ColdFusion Administrator, you define the interface to the applet, encapsulating it so that each invocation of the cfapplet tag is simple.

The cfapplet tag within a form offers several advantages over using the HTML applet tag:

  • Return values: The cfapplet tag requires a form field name attribute, so you can avoid coding additional JavaScript to capture the applet’s return values. You can reference return values like any other ColdFusion form variable: Form.variablename.

  • Ease of use: The applet’s interface is defined in the ColdFusion Administrator, so each instance of the cfapplet tag in your pages only needs to reference the applet name and specify a form variable name.

  • Parameter defaults: ColdFusion uses the parameter value pairs that you defined in the ColdFusion Administrator. You can override these values by specifying parameter value pairs in the cfapplet tag.

When an applet is registered, you enter just the applet source and the form variable name:

<cfapplet appletsource="Calculator"name="calc_value">

By contrast, with the HTML applet tag, you must declare all the applet’s parameters every time you want to use it in a ColdFusion page.

Registering a Java applet

Before you can use a Java applet in your ColdFusion pages, register the applet in the ColdFusion Administrator.

  1. Open the ColdFusion Administrator by clicking the Administrator icon in the ColdFusion Program group and entering the Administrator password.

  2. Under Extensions, click Java Applets.

    The Java Applets page appears.

  3. Click the Register New Applet button.

    The Add/Edit Applet page appears.

  4. Enter options in the applet registration fields, as described in the ColdFusion Administrator online help. Use the Add button to add parameters.

  5. Click Submit.

Using the cfapplet tag to embed an applet

After you register an applet, you can use the cfapplet tag to place the applet in a ColdFusion page. The cfapplet tag has two required attributes: appletsource and name. Because you registered the applet and you defined each applet parameter with a default value, you can run the applet with a simple form of the cfapplet tag:

<cfapplet appletSource="appletname" name="form_variable">

Overriding alignment and positioning values

To override any of the values defined in the ColdFusion Administrator for the applet, you can use the optional cfapplet parameters to specify custom values. For example, the following cfapplet tag specifies custom spacing and alignment values:

<cfapplet appletSource="myapplet" 
    name="applet1_var" 
    height=400 
    width=200 
    vspace=125 
    hspace=125 
    align="left">

Overriding parameter values

You can override the values that you assigned to applet parameters in the ColdFusion Administrator by providing new values for any parameter. To override a parameter, you must have already defined the parameter and its default value in the ColdFusion Administrator Applets page. The following example overrides the default values of two parameters, Param1 and Param2:

<cfapplet appletSource="myapplet" 
    name="applet1_var" 
    Param1="registered parameter1" 
    Param2="registered parameter2">

Handling form variables from an applet

The cfapplet tag name attribute corresponds to a variable in the action page, Form.appletname, which holds any value that the applet method returns when it is executed in the cfform tag.

Not all Java applets return values. For instance, graphical widgets might not return a specific value. For this type of applet, the method field in the ColdFusion Administrator remains empty, but you must still provide a cfappletname attribute.

You can only use one method for each applet that you register. If an applet includes more than one method that you want to access, you can register the applet with a unique name for each additional method you want to use.

Reference a Java applet return value in your application page

  1. Specify the name of the method in the Add/Registered Java Applet page of the ColdFusion Administrator.

  2. Specify the method name in the name attribute of the cfapplet tag.

When your page executes the applet, ColdFusion creates a form variable with the name that you specified. If you do not specify a method, ColdFusion does not create a form variable.