
746
Chapter 33: Integrating J2EE and Java Elements in CFML Applications
Using Java objects
You use the
cfobject
tag to create an instance of a Java object. You use other ColdFusion tags,
such as
cfset
and
cfoutput
, or CFScript to invoke properties (attributes), and methods
(operations) on the object.
Method 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. For more information on type conversion issues, see
“Java and ColdFusion data type conversions” on page 751
.
The examples in the following sections assume that the
name
attribute in the
cfobject
tag
specified the value obj, and that the object has a property called Property, and methods called
Method1, Method2, and Method3.
Note:
The
cfdump
tag displays an object’s public methods and data.
Using basic object techniques
The following sections describe how to invoke Java objects.
Invoking objects
The
cfobject
tag makes Java objects available in ColdFusion MX. It can access any Java class
that is available on the JVM classpath or in either of the following locations:
•
In a Java archive (.jar) file in
web_root
/WEB-INF/lib
•
In a class (.class) file in
web_root
/WEB-INF/classes
For example:
<cfobject type="Java" class="MyClass" name="myObj">
Although the
cfobject
tag loads the class, it does
not
create an instance object. Only static
methods and fields are accessible immediately after the call to
cfobject
.
If you call a public non-static method on the object without first calling the
init
method, there
ColdFusion makes an implicit call to the default constructor.
To call an object constructor explicitly, use the special ColdFusion
init
method with the
appropriate arguments after you use the
cfobject tag
; for example:
<cfobject type="Java" class="MyClass" name="myObj">
<cfset ret=myObj.init(arg1, arg2)>
Note:
The
init
method is
not
a method of the object, but a ColdFusion identifier that calls the
new
function on the class constructor. So, if a Java object has an
init
method, a name conflict exists and
you cannot call the object’s
init
method.
To have persistent access to an object, you must use the
init
function, because it returns a
reference to an instance of the object, and
cfobject
does not.
An object created using
cfobject
or returned by other objects is implicitly released at the end of
the ColdFusion page execution.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......