CFOBJECT Type="JAVA"

CFOBJECT allows you to create and use JAVA objects, and by extension EJB objects.

This support is currently only for NT, but will be extended to Solaris in the next release.

Syntax

<CFOBJECT 
    ACTION="Create"
    TYPE="Java"
    CLASS="Java class"
    NAME="object name"
>

ACTION

Required. Specifies "Create" in order to create the Java object or the WebLogic Environment.

TYPE

Required. Specifies that the type of object, in this case, this is always "Java."

CLASS

Required. Specifies the Java class.

NAME

Required. The name used within CFML to access the object.

Usage

To be able to call Java CFXs or Java objects, ColdFusion uses a JVM embedded in the process. The loading, location and the settings for the JVM are configurable using the ColdFusion Administrator pages.

Any Java class available in the class path specified in the ColdFusion Administrator can be loaded and used from ColdFusion using the CFOBJECT tag.

Use the following steps to access Java methods and fields:

1. Call CFOBJECT to load the class. See Example.

2. Use the init method with appropriate arguments to call a constructor explicitly. For example:

        <CFSET ret = myObj.init(arg1, arg2)>

Calling a public method on the object without first calling the "init" method results in an implicit call to the default constructor. Arguments and return values can be any valid Java type (simple, arrays, objects). ColdFusion does the appropriate conversions when strings are passed as arguments, but not when they are received as return values.

Overloaded methods are supported as long as the number of arguments are different. Future enhancements will let you use cast functions that will allow method signatures to be built more accurately.

Calling EJBs

To create and call all the appropriate EJB objects, use CFOBJECT. The sequence in the second example assumes that the Weblogic JNDI is used to register and find EJBHome instances.

Example of Java Object

<!----------------------------------------------------------------------
This CFOBJECT call loads the class MyClass but does not create an 
instance object. Static methods and fields are accessible after a call to 
CFOBJECT.
----------------------------------------------------------------------->
<CFOBJECT 
    ACTION="CREATE"
    TYPE="Java"
    CLASS="myClass"
    NAME="myObj"
>

Example of EJB

<!-------------------------------------------------------------
The CFOBJECT tag creates the Weblogic Environment object, which is then 
used to get the InitialContext. The context object is used to look up the 
EJBHome interface. The call to create() results in getting an instance of 
stateless session EJB.
----------------------------------------------------------------------->

<CFOBJECT 
    ACTION="CREATE"
    TYPE="JAVA"
    CLASS="weblogic/jndi/Environment"
    NAME="wlEnv">

<CFSET ctx = wlEnv.getInitialContext()>
<CFSET ejbHome = ctx.lookup("statelessSession.TraderHome")>                                         
<CFSET trader = ejbHome.Create()>                     
<CFSET value = trader.shareValue(20, 55.45)>                         
<CFOUTPUT>
     Share value = #value#
</CFOUTPUT>
<CFSET value = trader.remove()>