cfobject type = "java"

Description

Lets you create and use JAVA objects, and by extension EJB objects.

Category

Extensibility tags

Syntax

<cfobject 
  action = "Create"
  type = "Java"
  class = "Java class"
  name = "object name"> 

See also

cfcollection, cfexecute, cfgraph, cfindex, cfreport, cfsearch, cfservlet, cfwddx

Attributes

Attribute
Description
action
Required. Specifies "Create" in order to create the Java object or the WebLogic Environment.
type
Required. Specifies the type of object; always "Java."
class
Required. Specifies the Java class.
name
Required. The name used within CFML to access the object.

Usage

To call Java CFXs or Java objects, ColdFusion uses a JVM embedded in the process. The JVM loading, location and settings 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 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 if the number of arguments is different. Future enhancements will let you use cast functions to allow method signatures to be built more accurately.

Calling EJBs

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

Example 1

<!---------------------------------------------------------------------
Example of a 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 2

<!---------------------------------------------------------------------
Example of an 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()>