The CFOBJECT tag can call any Java class that's available on the Class Path specified in the ColdFusion Administrator. For example:
<CFOBJECT Type="Java" Class="MyClass" Name = "myObj">
Although this loads the class, it doesn't create an instance object. Static methods and fields are accessible after the call to CFOBJECT.
To call the constructors explicitly, use the init
method with the appropriate arguments. For example:
<CFSET ret=myObj.init(arg1, arg2)>
If you call a public method on the object without first calling the init
method, the result will be an implicit call to the default constructor. Arguments and return values can be any valid Java type, for example simple arrays and objects. ColdFusion does the appropriate conversions when strings are passed as arguments, but not when they are received as return values.
To call an EJB, you use CFOBJECT to create and call all the appropriate objects. In the following example, it is assumed that the Weblogic JNDI is used to register and find EJBHome instances:
<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()>
In this example, 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.
Exceptions thrown by Java object methods can be caught by the CFTRY and CFCATCH tags. ColdFusion checks to see if the exceptin thrown is the method exception and stores the class name of the eception in the message
field of the CFCATCH variable.