User Guide

Table Of Contents
Using Java objects 937
Example: CFML Java exception handling code
The following CFML code calls the testException class doException method. The
cfcatch block
handles the resulting exception.
<cfobject action=create type=java class=testException name=Obj>
<cftry>
<cfset Obj.doException() >
<cfcatch type="myException">
<cfoutput>
<br>The exception message is: #cfcatch.Message#<br>
</cfoutput>
</cfcatch>
</cftry>
Examples: using Java with CFML
The following sections show several examples of using Java objects in CFML. They include
examples of using a custom Java class, a standard Java API class in a user-defined function, a
JavaBean, and an Enterprise JavaBean (EJB).
Using a Java API in a UDF
The following example of a user-defined function (UDF) is functionally identical to the
GetHostAddress function from the NetLib library of UDFs from the Common Function
Library Project, www.cflib.org. It uses the
InetAddress class from the standard Java 2 java.net
package to get the Internet address of a specified host:
<cfscript>
function GetHostAddress(host) {
// Define the function local variables.
var iaddrClass="";
var address="";
// Initialize the Java class.
iaddrClass=CreateObject("java", "java.net.InetAddress");
// Get the address object.
address=iaddrClass.getByName(host);
// Return the address
return address.getHostAddress();
}
</cfscript>
<cfoutput>#gethostaddress("macromedia.com")#</cfoutput>