Calling Methods and Properties in a Java Class


The <cfset> tag creates variables and runs functions in a ColdFusion page. For example, to create an integer variable with the value 10, use the following code:

 <cfset intval=10> 

The <cfset> tag can also be used to call a method, retrieve the value of a property, or set a property in a Java object.

Handling Object Methods

A Java class can contain constructor methods, properties, and various other methods. You can use this code to call a constructor method after creating a Java object using the <cfobject> tag:

 <cfset ret=myObj.init(arg1)> 

Note

The init() method is used by ColdFusion to call the constructor method of the class. It's not the init() method defined in the Java class.

You can also use the <cfset> tag to call other methods defined in a Java class, as follows:

 <cfobject type="Java" action="Create"  name="obj"> <cfset obj.mymethod()> 

Handling Methods That Return Values

When you execute a method in a Java object, it may or may not return values. This code shows a Java class currdate containing a method retDate(), which returns the current system date:

 import java.io.*; import java.util.*; public class currdate {     public String retDate()     {       Date mydate =new Date();       String datestr= mydate.toString();       return (datestr);     } } 

The following code shows how to execute retdate() in a Java class using <cfobject>, retrieve the current date returned by retDate(), and display it in the Web browser:

 <HTML> <HEAD> <Title> The Current System Date</Title> </HEAD> <BODY>     <cfobject type="Java"  name="myObj"                 action="create">     <cfset ret=myObj.retDate()>     <cfoutput>       #ret#       </cfoutput> </BODY> </HTML> 

Passing Arguments to Methods

You can pass ColdFusion variables or values to methods in a Java object. This code shows a Java class concat containing the method showmessage() that accepts a single string argument and an integer argument. The method concatenates the two arguments and returns a String value:

 import java.io.*; public class concat {     int myage;     String mystr; public String newmethod (String arg_name, int arg_age) {     myage=arg_age;     mystr=arg_name;     return (" My name is " + mystr +". I am " + myage + "                years old"); } } 

The following code calls newmethod() in the Java class shown in the previous code. It passes two arguments to newmethod() and displays the value returned by the method in the Web browser:

 <HTML> <HEAD> <Title> Passing Arguments to Methods </Title> </HEAD> <BODY>     <cfobject type="Java"  name="myObj"               action="create">     <cfset ret=myObj.newMethod("Roberto",25)>     <cfoutput>       #ret#     </cfoutput> </BODY> </HTML> 

Handling Object Properties

Although a Java class may have many properties defined, ColdFusion can only access its public properties. You can use the <cfset> tag to retrieve the value of a public property defined in a Java class.

The following code retrieves the age property of a Java class myclass:

 <cfobject type="Java"  name="propObj" action="create"> <cfset ret = propObj.age > 

You can use the <cfset> tag to set the value of a property as follows:

 <cfobject type="Java"  name="propObj" action="create"> <cfset propObj.age = 24 > 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net