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 Java Applets page (under Extensions on the Server tab). In the Administrator, you define the interface to the applet, encapsulating it so that each invocation of the cfapplet tag is very simple.

The cfapplet tag offers several advantages over using the HTML applet 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, you must 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 Java Applets on Administrator Server tab to open the Java Applets page.
  3. Click the Register New Applet button to open the Add/Registered Java Applet page.
  4. Enter a name for the applet that you want to register. Enter the information your applet requires, and choose the height, width, vertical and horizontal space, and alignment that you want. Enter the Parameter names and their default values.
  5. Click Create to complete the process.

Applet registration fields

The following table explains the 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 compiled applet. The filename is relative to the code base URL. The *.class file extension is not required.
Method
Enter the name of a method in the applet that returns a string value. If you specify the method name in the cfapplet tag name attribute, the value returned by the method is available in the form's action page as Form.name. 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 that 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 typically provides 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 provides guidelines on valid entries.

Using cfapplet 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. Since you registered the applet, and you defined each applet parameter 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 that you assigned to applet parameters in the Administrator by providing new values for any parameter. 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 parameter1"

  Param2="registered parameter2">

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 returns when it is executed in the cfform.

Not all Java applets return values. For instance, many graphical widgets do not return a specific value; they do their flipping, spinning, fading, exploding, and that is all. 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 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.

To 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 you code your cfform.

When your page executes the applet, a form variable is created with the name that you specified. If you do not specify a method, no form variable is created.



Banner.Novgorod.Ru