# Embedding Java Applets

The CFAPPLET tag allows you to embed Java applets in a CFFORM. To use CFAPPLET, you must first register your Java applet using the ColdFusion Administrator Applets page. In the Administrator, you define the interface to the applet, encapsulating it so that each invocation of the CFAPPLET tag is very simple.

CFAPPLET offers several advantages over using the HTML APPLET tag:

- Return values -- Since CFAPPLET requires a form field name attribute, you can avoid having to code 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 -- Since the applet's interface is defined in the Administrator, each instance of the CFAPPLET tag in your pages only needs to reference the applet's name and specify a form variable name.
- Parameter options -- You can override parameter values you defined in the Administrator by specifying the parameter value pair in CFAPPLET. Unless overridden, ColdFusion uses the parameter value pairs you defined in the Administrator.

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

    <CFAPPLET APPLETSOURCE="Calculator"
        NAME="calc_value">

By contrast, with the HTML APPLET tag, you'd have to invoke all the applet's parameters every time you wanted to use it in a ColdFusion page.

## Registering a Java applet

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

| To register a Java applet:

1. Open the ColdFusion Administrator by clicking on the Administrator icon in the ColdFusion Program group and entering the Administrator password (if required).
2. Click the Applets button to open the Registered Applets page.
3. Enter a name for the applet you want to register and click Register New Applet. Enter the information your applet requires, and choose the height, width, vertical and horizontal space, and alignment you want.

Applet registration fields are explained in the following table.

Applet Registration Fields
Field | Description
--- | ---
Codebase | Enter the base URL of the applet: the directory that contains the applet components. The applet class files must be located within the web browser root directory. Example:
    http://servername/classes
Code | This is the name of the file that contains the applet subclass. The filename is relative to the codebase URL. The *.class file extension is not required.
Method | Enter the method name in the applet that returns a string value. You use this method name in the NAME attribute of the CFAPPLET tag to populate a form variable with the method's value. If the applet has no method, leave this field blank.
Height | Enter a measurement in pixels for the vertical space for the applet.
Width | Enter a measurement in pixels for the horizontal space for the applet.
Vspace | Enter a measurement in pixels for the space above and below the applet.
Hspace | Enter a measurement in pixels for the space on each side of the applet.
Align | Choose the alignment you want.
Java Not Supported Message | This message is displayed by browsers that do not support Java applets. If you want to override this message, you specify a different message in the CFAPPLET NOTSUPPORTED attribute.
Parameter Name | Enter a name for a required applet parameter. Your Java applet will typically provide the parameter name needed to use the applet. Enter each parameter in a separate parameter field.
Value | For every parameter you enter, define a default value. Your applet documentation will provide guidelines on valid entries.

Click Create to complete the process.

## Using CFAPPLET to embed an applet

Once you've registered 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. Since the applet has been registered, and each applet parameter defined with a default value, you can invoke the applet with a very 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 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 also override the values you assigned to applet parameters in the Administrator by providing new values for any parameter. Note that in order to override a parameter, you must have already defined the parameter and a default value for it in the ColdFusion Administrator Applets page.

    <CFAPPLET APPLETSOURCE="myapplet"
        NAME="applet1_var"
        Param1="registered parameter"
        Param2="registered parameter">

## Handling form variables from an applet

The CFAPPLET tag requires you to specify a form variable name for the applet. This variable, referenced like other ColdFusion form variables, `form.`variable_name holds the value the applet method provides when it is executed in the CFFORM.

Not all Java applets return values. Some, like many graphical widgets, do not return a specific value; they do their flipping, spinning, fading, exploding, and that's that. For this kind of applet, the method field in the Administrator remains empty. Other applets, however, do have a method that returns a value. You can only use one method for each applet you register. If an applet includes more than one method that you want to access, you can register the applet with a unique name an additional time for each method you want to use.

| To reference a Java applet return value in your application page:

1. Specify the name of the method in the Register New Applet page of the ColdFusion Administrator.
2. Specify the method name in the NAME attribute of the CFAPPLET tag when you code your CFFORM.

When your page executes the applet, a form variable is created with the name you specified. If you don't specify a method, no form variable is created.