Whatever message this page gives is out now! Go check it out!
<cfobject type="Java" class="MyClass" name="myObj"> |
<cfset ret=myObj.init(arg1, arg2)> |
<cfset retVal = obj.Method1()> |
<cfset retVal = obj.Method1(x, "a string literal")> |
There are currently #myFish.TotalFish# fish in the tank. </cfoutput> |
<cfset myFish.GuppyCount = myFish.GuppyCount + 1> |
<cfset prop = myObj.X.Property>. |
public String FirstName; public String LastName; private float Salary; private int JobGrade; public Employee() { FirstName =""; LastName =""; Salary = 0.0f; JobGrade = 0; } public Employee(String First, String Last) { FirstName = First; LastName = Last; Salary = 0.0f; JobGrade = 0; } public Employee(String First, String Last, float salary, int grade) { FirstName = First; LastName = Last; Salary = salary; JobGrade = grade; } public void SetSalary(float Dollars) { Salary = Dollars; } public float GetSalary() { return Salary; } public void SetJobGrade(int grade) { JobGrade = grade; } public void SetJobGrade(String Grade) { if (Grade.equals("CEO")) { JobGrade = 3; } else if (Grade.equals("MANAGER")) { JobGrade = 2; } else if (Grade.equals("DEVELOPER")) { JobGrade = 1; } } public int GetJobGrade() { return JobGrade; } } |
<body> <cfobject action="create" type="java" class="Employee" name="emp"> <!--- <cfset 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> |
Code | Description | |
| Loads the Employee Java class and gives it an object name of emp . | |
| Does not call a constructor. ColdFusion calls the default constructor when it first uses the class; in this case, when it processes the next line. | |
| Sets the public fields in the emp object to your values. | |
| Gets the field values back from emp object. | |
| Displays the retrieved values. |
<body> <cfobject action="create" type="java" class="Employee" name="emp"> <cfset emp.init("John", "Doe", 100000.00, 10)> <cfset firstname=emp.firstname> <cfset lastname=emp.lastname> <cfset salary=emp.GetSalary()> <cfset grade=emp.GetJobGrade()> <cfoutput> Employee name is #firstname# #lastname#<br> Employee salary #DollarFormat(Salary)#<br> Employee Job Grade #grade# </cfoutput> </body> </html> |
CFML | Java |
Integer | short, int, long (short and int can result in a loss of precision). |
Real number | float double (float can result in a loss of precision. |
Boolean | boolean |
Date-time | java.util.Date |
String, including lists | Stringshort, int, long, float, double, java.util.Date, when a CFML string represents a number or date.boolean, for strings with the value Yes, No, True, and False (case-insensitive). |
Array | java.util.Vector (ColdFusion Arrays are internally represented using an instance of a java.util.Vector object.)ColdFusion can also map a CFML array to any of the following when the CFML array contains consistent data of a type that can be converted to the data type of the Java arr: byte[], char[], boolean[], int[], long[], float[], double[], String[], or Object[]. When a CFML array contains data of different of types, the conversion to a simple array type could fail. |
Structure | java.util.Map |
Query object | util.map and vectors together. If you have dynamic column names, you can retrieve it as queryname[column name][current row]. |
XML document object | Not supported. |
ColdFusion component | Not applicable. |
Java | CFML |
boolean/Boolean | Boolean |
byte/Byte | String |
char/Char | String |
short/Short | Integer |
int/Integer | Integer |
long/Long | Integer |
float/Float | Real Number |
double/Double | Real Number |
String | String |
java.util.Date | Date-time |
java.util.List | Comma-delimited list |
byte[] | Array |
char[] | Array |
boolean[] | Array |
String[] | Array |
java.util.Vector | Array |
java.util.Map | Structure |
<cfset emp.SetJobGrade("1")> |
<cfset emp.SetJobGrade(JavaCast("int", "1"))> |
//class myException
public class myException extends Exception
{
public myException(String msg) {
super(msg);
}
public myException() {
super("Error Message from myException");
}
}public testException () { } public void doException() throws myException { throw new myException("Throwing an exception from testException class"); } } |
<cftry> <cfset Obj.doException() > <cfcatch type="myException"> <cfoutput> <br>The exception message is: #cfcatch.Message#<br> </cfoutput> </cfcatch> </cftry> |
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("adobe.com")#</cfoutput> |
<head> <title>cfobject Test</title> </head> <body> <H1>cfobject Test</H1> <!--- Create the Context object to get at the static fields. ---> <CFOBJECT action=create name=ctx type="JAVA" class="javax.naming.Context"> <!--- Create the Properties object and call an explicit constructor---> <CFOBJECT action=create name=prop type="JAVA" class="java.util.Properties"> <!--- Call the init method (provided by cfobject) to invoke the Properties object constructor. ---> <cfset prop.init()> <!--- Specify the properties These are required for a remote server only ---> <cfset prop.put(ctx.INITIAL_CONTEXT_FACTORY, "jrun.naming.JRunContextFactory")> <cfset prop.put(ctx.PROVIDER_URL, "localhost:2908")> <!--- <cfset prop.put(ctx.SECURITY_PRINCIPAL, "admin")> <cfset prop.put(ctx.SECURITY_CREDENTIALS, "admin")> ---> <!--- Create the InitialContext ---> <CFOBJECT action=create name=initContext type="JAVA" class="javax.naming.InitialContext"> <!--- Call the init method (provided through cfobject) to pass the properties to the InitialContext constructor. ---> <cfset initContext.init(prop)> <!--- Get reference to home object. ---> <cfset home = initContext.lookup("SimpleBean")> <!--- Create new instance of entity bean. (hard-wired account number). Alternatively, you would use a find method to locate an existing entity bean. ---> <cfset mySimple = home.create()> <!--- Call a method in the entity bean. ---> <cfset myMessage = mySimple.getMessage()> <cfoutput> #myMessage#<br> </cfoutput> <!--- Close the context. ---> <cfset initContext.close()> </body> </html> |
Method | Description |
ReverseString | Reverses the order of a string. |
ReverseStringArray | Reverses the order of elements in an array of strings. |
Add | Overloaded: Adds and returns two integers or floats or adds the mPublicInt members of two Example class objects and returns an Example class object. |
SumArray | Returns the sum of the elements in an integer array. |
SumObjArray | Adds the values of the mPublicInt members of an array of Example class objects and returns an Example class object. |
ReverseArray | Reverses the order of an array of integers. |
Flip | Switches a Boolean value. |
public int mPublicInt; public Example() { mPublicInt = 0; } public Example(int IntVal) { mPublicInt = IntVal; } public String ReverseString(String s) { StringBuffer buffer = new StringBuffer(s); return new String(buffer.reverse()); } public String[] ReverseStringArray(String [] arr) { String[] ret = new String[arr.length]; for (int i=0; i < arr.length; i++) { ret[arr.length-i-1]=arr[i]; } return ret; } public int Add(int a, int b) { return (a+b); } public float Add(float a, float b) { return (a+b); } public Example Add(Example a, Example b) { return new Example(a.mPublicInt + b.mPublicInt); } static public int SumArray(int[] arr) { int sum=0; for (int i=0; i < arr.length; i++) { sum += arr[i]; } return sum; } static public Example SumObjArray(Example[] arr) { Example sum= new Example(); for (int i=0; i < arr.length; i++) { sum.mPublicInt += arr[i].mPublicInt; } return sum; } static public int[] ReverseArray(int[] arr) { int[] ret = new int[arr.length]; for (int i=0; i < arr.length; i++) { ret[arr.length-i-1]=arr[i]; } return ret; } static public boolean Flip(boolean val) { System.out.println("calling flipboolean"); return val?false:true; } } |
<head> <title>CFOBJECT and Java Example</title> </head> <body> <!--- Create a reference to an Example object ---> <cfobject action=create type=java class=Example name=obj> <!--- Create the object and initialize its public member to 5 ---> <cfset x=obj.init(JavaCast("int",5))> <!--- Create an array and populate it with string values, then use the Java object to reverse them. ---> <cfset myarray=ArrayNew(1)> <cfset myarray[1]="First"> <cfset myarray[2]="Second"> <cfset myarray[3]="Third"> <cfset ra=obj.ReverseStringArray(myarray)> <!--- Display the results ---> <cfoutput> <br> original array element 1: #myarray[1]#<br> original array element 2: #myarray[2]#<br> original array element 3: #myarray[3]#<br> after reverseelement 1: #ra[1]#<br> after reverseelement 2: #ra[2]#<br> after reverseelement 3: #ra[3]#<br> <br> </cfoutput> <!--- Use the Java object to flip a Boolean value, reverse a string, add two integers, and add two float numbers ---> <cfset c=obj.Flip(true)> <cfset StringVal=obj.ReverseString("This is a test")> <cfset IntVal=obj.Add(JavaCast("int",20),JavaCast("int",30))> <cfset FloatVal=obj.Add(JavaCast("float",2.56),JavaCast("float",3.51))> <!--- Display the results ---> <cfoutput> <br> StringVal: #StringVal#<br> IntVal: #IntVal#<br> FloatVal: #FloatVal#<br> <br> </cfoutput> <!--- Create a two-element array, sum its values, and reverse its elements ---> <cfset intarray=ArrayNew(1)> <cfset intarray[1]=1> <cfset intarray[2]=2> <cfset IntVal=obj.sumarray(intarray)> <cfset reversedarray=obj.ReverseArray(intarray)> <!--- Display the results ---> <cfoutput> <br> IntVal1 :#IntVal#<br> array1: #reversedarray[1]#<br> array2: #reversedarray[2]#<br> <br> </cfoutput><br> <!--- Create a ColdFusion array containing two Example objects. Use the SumObjArray method to add the objects in the array Get the public member of the resulting object---> <cfset oa=ArrayNew(1)> <cfobject action=create type=java class=Example name=obj1> <cfset VOID=obj1.init(JavaCast("int",5))> <cfobject action=create type=java class=Example name=obj2> <cfset VOID=obj2.init(JavaCast("int",10))> <cfset oa[1] = obj1> <cfset oa[2] = obj2> <cfset result = obj.SumObjArray(oa)> <cfset intval = result.mPublicInt> <!--- Display the results ---> <cfoutput> <br> intval1: #intval#<br> <br> </cfoutput><br> </body> </html> |