Example: CFML Java exception handling code
The following CFML code calls the testException class doException method. The
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
Library Project, http://www.cflib.org. It uses the
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>
Using an EJB
ColdFusion MX can use EJBs that are served by JRun 4.0 servers. The JRun server jrun.jar file
must have the same version as the jrun.jar file in ColdFusion.
To call an EJB, you use
you can use an EJB you must do the following:
Have a properly deployed EJB running on a J2EE server. The bean must be registered with the
1
JNDI server.
Have the following information:
2
Name of the EJB server
Port number of the JNDI naming service on the EJB server
754
Chapter 33: Integrating J2EE and Java Elements in CFML Applications
function from the NetLib library of UDFs from the Common Function
cfobject
type="Java"
class from the standard Java 2
InetAddress
to create and call the appropriate objects. Before
block
cfcatch
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers