User Guide

Table Of Contents
234 Chapter 10: Building and Using ColdFusion Components
To invoke a packaged component method using the cfinvoke tag:
1.
In your web root directory, create a directory named appResources.
2.
In the appResources directory, create a directory named components.
3.
Copy the tellTime2.cfc file you created in “Invoking methods of a CFC instance” on page 218
and the getUTCTime.cfm file that you created in “Putting executable code in a separate file
on page 207 to the components directory.
4.
Create the timeDisplay.cfm file with the following content and save it in your web root
directory:
<!--- Create the component instance. --->
<cfobject component="appResources.components.tellTime2" name="tellTimeObj">
<!--- Invoke the methods. --->
<cfinvoke component="#tellTimeObj#" method="getLocalTime"
returnvariable="localTime" >
<cfinvoke component="#tellTimeObj#" method="getUTCTime"
returnvariable="UTCTime" >
<!--- Display the results. --->
<h3>Time Display Page</h3>
<cfoutput>
Server's Local Time: #localTime#<br>
Calculated UTC Time: #UTCTime#
</cfoutput>
You use dot syntax to navigate directory structures. Place the directory name before the
component name.
5.
Browse the timeDisplay.cfm file in your browser.
The following example shows a CFScript invocation:
<cfscript>
helloCFC = createObject("component", "appResources.components.catQuery");
helloCFC.getSaleItems();
</cfscript>
The following example shows a URL invocation:
http://localhost/appResources/components/catQuery.cfc?method=getSalesItems
Using CFCs in persistent scopes
You can put a CFC instance in the Session or Application scope. This way, the component
properties continue to exist while the scope persists. For example, you might want to use a CFC
for a shopping cart application, where the shopping cart contents must persist for the length of
the user’s session. If you put the shopping cart CFC in the Session scope, you can use component
properties to store the cart contents. For example, the following line creates an instance of the
shoppingCart component in the Session scope:
<cfobject name="Session.myShoppingCart" component="shoppingCart">