|
To use Java in ColdFusion effectively, you need to handle the methods and properties defined in a Java class. Let's look at how to use the properties and methods in a simple Employee class using ColdFusion. The Employee class contains three properties, listed in Table C.2.
Property | Use | Public or Private |
---|---|---|
FirstName | Retrieves or modifies the first name of an employee | Public |
LastName | Retrieves or modifies the last name of an employee | Public |
Salary | Retrieves the salary of an employee | Private |
The Employee class contains the two methods listed in Table C.3.
Method | Use | Public or Private |
---|---|---|
GetSalary() | Returns the current salary of an employee | Public |
SetSalary() | Sets the current salary of an employee; accepts the salary value as an argument | Public |
To create the Employee class and use it in your ColdFusion page, perform the following steps:
Create the Employee.java file containing this code for the Employee class:
public class Employee { public String FirstName; public String LastName; private float Salary; public void Employee() { FirstName =""; LastName =""; Salary = 0.0f; } public void SetSalary(float salary_val) { Salary = salary_val; } public float GetSalary() { return Salary; } }
Note | You need to install the Java Software Development Kit (SDK) on your computer to compile a Java source code file to a Java class. |
Save this code as Employee.java and compile it using the Java compiler javac.exe.
Place Employee.class in \CfusionMX\wwwroot\WEB-INF\classes.
To use the Employee class, create a ColdFusion page with the following code:
<html> <body> <cfobject action="create" type="java" name="employee"> <!-- Set the first name and last name of the employee by setting the firstname and lastname property of the Employee class --> <cfset employee.firstname="Jonathan"> <cfset employee.lastname="Cruise"> <!-- Display the name of the Employee --> <cfset firstname=employee.firstname> <cfset lastname=employee.lastname> <CFOUTPUT> The name of the employee is #firstname# #lastname#.<br> </CFOUTPUT> <!-- Set the salary of the employee --> <cfset employee.SetSalary(24456.23)> <!-- Get the current salary of the employee --> <cfset salaryval= employee.GetSalary()> </body> <! -- Display the name of the employee --> <cfoutput> The salary of this employee is US$ #salaryval# </cfoutput> </html>
Save the ColdFusion page as callingjava.cfm.
View the ColdFusion page using the Web browser by typing this URL:
http://computer_name:8500/callingpage.cfm
Here, computer_name is the name of the computer where ColdFusion MX server is installed.
The Web browser displays the output, as shown in Figure C.3.
Figure C.3: Calling and using a Java class in a ColdFusion page.
You can also access and create a Java object in CFScript using the CreateObject() function. This function accepts two arguments:
Type. The type of the object to be created. For example, specify the type argument as Java to create a Java object.
Class. The name of the Java class to be used in CFScript.
The following code uses CreateObject() to call and use a Java class called Employee in CFScript:
<HTML> <HEAD></HEAD> <BODY> <CFSCRIPT> myclass= CreateObject("java","Employee"); myclass.Firstname="Robert"; WriteOutput (myclass.Firstname); </CFSCRIPT> </BODY> </HTML>
|