User Guide

380 Chapter 20 Using cfobject to Invoke Component Objects
Example: CFML page that uses the Employee class
Save the following text as JEmployee.cfm:
<html>
<body>
<cfobject action=create type=java class=Employee name=emp>
<!-- <cfset void = emp.init()> -->
<cfset emp.firstname="john">
<cfset emp.lastname="doe">
<cfset firstname=emp.firstname>
<cfset lastname=emp.lastname>
</body>
<cfoutput>
Employee name is #firstname# #lastname#
</cfoutput>
</html>
When you view the page in your browser, you should get the following output:
Employee name is john doe
Reviewing the code
The following table describes the CFML code and its function:
Java considerations
Keep the following points in mind when you write a ColdFusion page that uses a Java
class object:
The Java class name is case sensitive. You must make sure that both the Java code
and the CFML code use Employee as the class name.
Code Description
<cfobject action=create
type=java class=Employee
name=emp>
Load an instance of the Employee Java
class named emp.
<!--- <cfset void=emp.init()>
--->
Do not call a constructor. ColdFusion
invokes the default constructor when it first
uses the class; in this case, when it
processes the next line.
<cfset emp.firstname="john">
<cfset emp.lastname="doe">
Set the public fields in the emp object to your
values.
<cfset firstname=emp.firstname>
<cfset lastname=emp.lastname>
Get the field values back from emp object.
<cfoutput>
Employee name is #firstname#
#lastname#
</cfoutput>
Display the retrieved values.