Appendix C: Using Java Objects in ColdFusion MX


ColdFusion MX implements code reusability by incorporating functionality from other technologies, such as Java or COM, into a ColdFusion application. For example, consider a Java class that connects with an Oracle database and performs a certain task on the data stored in the database. ColdFusion lets you call and use the Java class directly in your page to incorporate its functionality in a ColdFusion page.

Creating an Instance of a Java Object

To create an instance of a Java object in your ColdFusion page, you need to use the <cfobject> tag. You can use the instance of the Java object to retrieve and modify attribute values and call methods and functions defined in the Java class.

Accessing Java Classes Using ColdFusion MX Server

You can use the <cfobject> tag to create an object instance of the Java class. To use the instance in a ColdFusion page, place the Java classes in any one of these locations:

  • In a class file in the CfusionMX\wwwroot\WEB-INF\classes directory

  • In a Java Archive (.jar) in the CfusionMX\wwwroot\WEB-INF\lib directory

  • In a directory on the computer specified as the Java Virtual Machine (JVM) class path

Note

ColdFusion Server comes with a JVM that calls or runs a Java object to be used by ColdFusion tags, such as <cfset>, in a ColdFusion page.

To use ColdFusion Administrator to specify the JVM class path where you can store the class files that you want to use in your ColdFusion page, perform the following steps:

  1. Launch ColdFusion Administrator from the Windows Start menu by choosing Start, Programs, Macromedia ColdFusion MX, Administrator. This opens the ColdFusion Administrator window, divided into two frames, in your Web browser.

  2. Click the Java and JVM link under the SERVER SETTINGS heading in the left frame of the ColdFusion Administrator window. The Java and JVM Settings page appears in the right frame of the ColdFusion Administrator window, as shown in Figure C.1.

    click to expand
    Figure C.1: The Java and JVM Settings page.

  3. In the Class Path box, type the path of the directory where you'll store your Java classes, such as d:\myclasses. Figure C.2 shows how to specify the JVM class path in ColdFusion Administrator.

    click to expand
    Figure C.2: Setting the JVM class path.

  4. Click Submit Changes. You need to stop and restart ColdFusion MX Server for the new settings to take effect.

start sidebar
STOPPING AND RESTARTING COLDFUSION MX SERVER IN WINDOWS 2000

To stop and restart ColdFusion MX Server in Windows 2000:

  1. From the Windows Start menu, select Start, Settings, Control Panel to open the Windows Control Panel.

  2. Double-click the Administrative Tools icon.

  3. Double-click the Services icon in the Administrative Tools window to view all the services running in the computer.

  4. Select ColdFusion MX Application Server in the right pane of the Services window.

  5. Right-click and choose Stop from the pop-up menu to stop the ColdFusion MX Application Server.

  6. Finally, right-click again and choose Start from the pop-up menu to restart the ColdFusion MX Application Server.

end sidebar

Using the <cfobject> Tag

The <cfobject> tag creates an object instance in a ColdFusion page. This tag can be used to create the types of objects listed in Table C.1.

Table C.1: Objects That Can Be Created Using the <cfobject> Tag

Object Type

Description

Component Object Model (COM) object

Creates an instance of a Microsoft COM object that can be an ActiveX Dynamic Link Library (DLL)

cfcomponent object

Creates a ColdFusion component object in a ColdFusion page

Java object

Creates an object instance of a Java class in a ColdFusion page

The <cfobject> tag uses the type attribute to determine the type of object, COM or Java, within a ColdFusion page. You can use this tag to create a Java object by specifying the value of the type attribute as Java. Here's the syntax of the <cfobject> tag:

 <cfobject type="Java"     action="Create"     class= "Java_class_name"     name="object_name"> 

The preceding syntax contains these attributes:

  • type specifies the type of the object to be created. For example, if you specify that the value of the type attribute is Java, the <cfobject> tag creates a Java object.

  • action creates the instance of the Java object.

  • class specifies the name of the Java class to be created in the ColdFusion page.

  • name specifies the name of the object, which is used to call a method or retrieve the value of a property defined in the object. This name is used in a ColdFusion page as a variable identifier.

This code uses the <cfobject> tag to create an object instance of a Java class in a ColdFusion page:

 <cfobject type="Java" action="Create"  name="myobj"> 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net