Calling Java Objects; Calling Ejbs; Exception Handling - MACROMEDIA COLDFUSION 4.5-DEVELOPING WEB Develop Manual

Developing web applications with coldfusion
Table of Contents

Advertisement

Chapter 19: Using CFOBJECT to Invoke Component Objects

Calling Java Objects

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
arguments. For example:
<CFSET ret=myObj.init(arg1, arg2)>
If you call a public method on the object without first calling 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.

Calling EJBs

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"
<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.

Exception handling

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
TYPE="Java"
CLASS="weblogic/jndi/Environment"
NAME="wlEnv">
method with the appropriate
init
init
field of the CFCATCH variable.
message
335
method, the

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 4.5

Table of Contents