Invoking Objects


CreateObject() is a functional equivalent of the <cfobject> tag. As such, it is a way to call ColdFusion Components, CORBA, COM, Java Classes, and Enterprise Java Beans (EJB) from ColdFusion and is well suited for ColdFusion's scripting environment.

The <cfobject> tag will be covered in Chapter 33, "Java, COM, and CORBA."


Let's take Java into consideration as our example. An object in Java is created when the CreateObject() function is called and the first argument is passed as "JAVA" with a second argument as the class name. This loads the class into memory but does not call the constructor methods of the class. A constructor method is the section of Java that registers an instance of the object and appropriates memory for all its properties and methods. Because you can have more than one constructor method, you must either call the default constructor or initialize one directly. You call a specific constructor by referencing the object name and calling a special method known as init.

The following example calls a Java object called BankAccount.

 <cfscript> // load the object into memory BankAcc = CreateObject("java","BankAccount"); // now initialize (call the constructor) of BankAccount BankAcc.init("John Doe","Checking Account Deposit","$50"); // now call some other method of the object BankAcc.SendMailer("Welcome New Customer Mailer"); // Set properties of the bank account if (customerCode is "no risk")  BankAcc.overdraftProtection = "True"; </cfscript> 

Based on the preceding code, the Java class is called and invoked from a ColdFusion script block. Parameters and methods of the object can be accessed using the object name returned by the CreateObject() function.

Because ColdFusion Components (CFC) may be accessed as objects using the CreateObject() function, CFCs are accessible within <cfscript> code.

ColdFusion Components are covered in Chapter 31, "ColdFusion Components."


TIP

CFC code can invoke any tag, so it is indeed possible to use tags within <cfscript>, albeit indirectly, via CFCs.




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

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