Accessibility
 

ColdFusion Article

Icon or Spacer Icon or Spacer Icon or Spacer
Tom Muck

Tom Muck
MX inSite
 

Switching from ASP to ColdFusion


If you're an ASP developer looking to learn Macromedia ColdFusion, you'll  find it's an easy transition. Though the two technologies are very different, a basic understanding of ASP provides many of the skills required in making the change. The reasons for changing are varied?maybe a project requires or you to change languages, or maybe you wish to increase your productivity.  This article addresses both topics and explains some of the new features that make Macromedia ColdFusion MX even more attractive for developers.

Creating objects
ASP is based on scripting languages, such as VBScript and JScript. These languages are the glue that holds an ASP page together. Scripting allows you to instantiate server-side objects, which add dynamic content to your HTML pages. If you've used scripting languages, you?ll find that the ColdFusion language (ColdFusion Markup Language, or CFML) is quite a bit different. First of all, ColdFusion objects (tags) are all inherently active. With ASP, you must create an instance of an object, use it, and then destroy it. With ColdFusion, you use ColdFusion tags, but you don't need to create or destroy them.  When you put a CFML tag on a page, the ColdFusion server instantiates it, and takes care of the cleanup.  The code samples below demonstrate the differences between ASP and ColdFusion.

With ASP, you would use the following code to create a recordset object:

<%
set rsGetOrders = Server.CreateObject("ADODB.Recordset")
rsGetOrders.ActiveConnection = "northwind"
rsGetOrders.Source = "SELECT CustomerID, OrderDate from Orders" 
rsGetOrders.CursorLocation = 2
rsGetOrders.LockType = 3
rsGetOrders.Open()
rsGetOrders_numRows = 0
%>

You use the recordset on a page by manually looping through your resultset (note that the following is VBScript):

<table>
<%
While (NOT rsGetOrders.EOF))
%>
        <tr>
          <td><%=(rsGetOrders.Fields.Item("CustomerID").Value)%></td>
          <td><%=(rsGetOrders.Fields.Item("OrderDate").Value)%></td>
        </tr>
<%
rsGetOrders.MoveNext()
Wend
%>
</table>
              

Finally, when you're done with the resultset, you destroy the object and clear the memory space on the server (in this example, you're setting rsGetOrders = Nothing) as follows:

<%
rsGetOrders.Close()
Set rsGetOrders = Nothing
%>

Now, we'll do the same thing with ColdFusion code.  Remember that ColdFusion tags are the equivalent of a server-side object in ASP. To perform the same process in ColdFusion, use the cfquery tag instead of the recordset object you would have used in ASP:

<cfquery name= "rsGetOrders" datasource= "northwind">
SELECT CustomerID, OrderDate FROM Orders
</cfquery>

This code creates the recordset. Displaying it is simple with ColdFusion, since looping is built into the ColdFusion tags.  It?s unnecessary to manually start or stop the loop as you did in ASP. You simply print the results with a cfoutput tag, as follows:

<table>
			  
<!---The cfoutput query prints the query results.  Notice that the query
attribute specifies the query that it is outputting--->

<cfoutput query="rsGetOrders">
        <tr>
          <td>#rsGetOrders.CustomerID#</td>
          <td>#rsGetOrders.OrderDate#</td>
        </tr>
</cfoutput>
</table>

The ColdFusion code has several advantages that are evident right from the start:

  • It's shorter and more concise.
  • It's easier to understand exactly what the code does, even without programming knowledge.
  • You don't need to write cleanup code, and you don?t have to destroy your objects, because the ColdFusion server handles those details. 
  • Although it's not apparent from the code, you can use the recordset again and again on the same page without resetting your pointers.  ASP would required you to use rsGetOrder.MoveFirst() to reset the pointer to perform the same operation.
Using less code
Less code = less typing = more time for programming the meat of your program.  With ColdFusion, you're not spending as much time on the mundane aspects, such as object creation, keeping track of loops, and the closing and destroying of objects.  ColdFusion works like this across the board. Take another example, sending e-mail.  To send e-mail with ColdFusion, use the following code:
<cfmail to="#form.tofield#"
 from="admin@mywebsite.com" 
 subject="Your order has been placed">
<cfinclude template="message.cfm">
</cfmail>

Note that you would need to specify a mail server in the ColdFusion administrator.  This is as simple as entering the mail server IP or internet address in the appropriate setting in the ColdFusion administrator. 

If you've ever sent e-mail from ASP, you know what's required. First, you'd have to know which e-mail component is registered on the server. This could be CDO, JMail, ASPMail, or any number of other mail components. You may even have a host that doesn?t have a mail component installed. In any case, each component has a different syntax for instantiating the object and a different syntax for using e-mail.  Needless to say, coding ASP for sending e-mail is involved.

Also note that in the ColdFusion example, that the body of the message is included as a separate file. This could just as easily have been hard-coded into the program, but with ColdFusion, you can include files with the cfinclude tag.  This can be a great, added bonus. For one thing, you can create conditional logic for the including the files.  In other words, you can access different includes files based on criteria that you establish, such as a value in a field from the database. You cannot do this in ASP, however.  For example, inside the ColdFusion message.cfm page, you might have code that appears as follows:

<cfswitch expression = #rsGetAllEmails.emailtext#>
     <cfcase value="A05">
          <cfinclude template="orders_5day.cfm">
     </cfcase> 
     <cfcase value="A15">
          <cfinclude template="orders_15day.cfm">
     </cfcase>
     <cfcase value="A30">
          <cfinclude template="orders_30day.cfm">
     </cfcase>
     <cfcase value="A45">
          <cfinclude template="orders_45day.cfm">
     </cfcase>
</cfswitch>

Using conditional includes can be extremely useful in maintaining your application. Using include files in ColdFusion causes no performance degradation, so you can package your logic within them.  Later, you'll see how you can make your code even more flexible in the discussion on ColdFusion Components, a new feature in ColdFusion MX.

ASP objects
ColdFusion has always had a rich set of built-in tags to work with, but as an added bonus, you can also use traditional ASP objects in ColdFusion by using the cfobject tag or the CreateObject() function within the cfscript tags.  For example, you can use components you created in Visual Basic for your ASP pages on your ColdFusion pages too. ColdFusion creates the objects using the cfobject tag and accesses the properties and methods using the cfscript  tag in the same way that you would do so with an ASP scripting language. The bonus here is that if you already have objects that contain your business logic, you can use those objects in ColdFusion, thereby shortening the time required to create your application in ColdFusion.

One thing that will become apparent after you've created a couple of ColdFusion sites -- most of the functionality that requires the use of objects, or third party components in ASP, is built into the ColdFusion language using the tag-based syntax. A few of the more powerful ColdFusion tags (some of them are new to ColdFusion MX) are listed below:


CFCOMPONENT Creates and defines a component object; encloses functionality that you build in CMFL within in cffunction tags and makes it available to any ColdFusion page on your ColdFusion Server or other remote services, such as web services, Macromedia Flash MX, and so forth.  New feature in ColdFusion MX.  Read more about this in the ColdFusion Developer Center
CFCHART Create complex charts and graphs in Flash, JPG, or PNG on the fly from your database queries (or other types of queries as well).  New feature in ColdFusion MX.  Read more about this in Tim Buntel's Basic Charting and Graphing with ColdFusion MX.
CFXML Create an XML document easily.  Additionally, new ColdFusion XML functions enable you to parse and search XML easily.  New feature in ColdFusion MX.  Read more about this in Nate Weiss's Utilizing XML and XSLT in ColdFusion MX PDF (528k).
CFDUMP Dumps a complete variable, structure, or query to the page for debugging your CF pages. It?s especially handy for dumping a complete result set to the page in a nicely formatted table.  Helpful in testing and debugging.
CFDIRECTORY Gives you complete access to any directory on your hard drive for listing, creating, deleting, or renaming directories.
CFFILE Gives you complete access to files on your server, including the ability to upload, move, rename, copy, delete, read, read binary, write, or append, all from one simple tag.
CFREGISTRY Read and write to the Windows registry.
CFEXECUTE Executes a program, batch file, or other executable on the server.
CFPOP Manage and retrieve e-mail messages from a POP email server.

New features in ColdFusion MX
ColdFusion MX is a new release of a die-hard product that has been completely redesigned from the ground up. This version of ColdFusion is Java-based, but works almost entirely like the previous versions of ColdFusion. ColdFusion tags -- with very few exceptions -- work in exactly the same manner as described in this article. In fact, most applications created in previous versions of ColdFusion will run without  modification.

One of the biggest new developments in ColdFusion MX is that you can use JSP tag libraries with your ColdFusion pages. This means that a ColdFusion programmer can take advantage of any existing tag libraries, as well as create new ones. This is on top of all the ColdFusion tags and COM objects that are already in a ColdFusion programmer?s arsenal.

Another new, big feature in ColdFusion MX is ColdFusion Components (CFCs).  CFCs are self-documenting and  self-contained.  You can use CFCs through any ColdFusion pages on the ColdFusion server.  Other technologies can access your ColdFusion components if you publish them as web services (for instance, a Macromedia Flash application may use a ColdFusion component to run a database query based on a user's request).

Create CFCs with the cfcomponent tag, and save them with a CFC extension.  Include your CFML logic (such as a cfquery tag, conditional code, and so forth) within a cffunction tag inside of the cfcomponent tag inside the CFC file.  Then, any ColdFusion page on your server can call the functions (also called methods) within the CFC.  For full details on how to use CFCs, refer to the other articles in the ColdFusion Developer Center, and to the ColdFusion MX documentation book, Developing ColdFusion MX applications

Developing ColdFusion components that produce web services has one major advantage over producing web services in other technologies ? you can create a ColdFusion component web service with standard CFML code in a fraction of the time.  Simply add the attribute access=remote to the cffunction tag to activate the CFC as a web service.  The following is a simple CFC that reads the contents of a directory and returns a list of JPG files in that directory:

><cfcomponent>
  <cffunction name="getJPGList" returnType="string">
    <cfdirectory name="fileList"
       action="list"
       directory="H:\public_jpegs" 
       filter="*.jpg"> 
   <cfreturn #ValueList(fileList.name)#>
  </cffunction>
</cfcomponent>

Save this file as getFiles.cfc (Note: You'll need to change the directory based on your directory structure). You can use this CFC on your ColdFusion page to get a comma-separated list of all JPEG files in the H:\public_jpegs directory. To invoke the CFC on a ColdFusion page, use this simple code:

<cfinvoke component="getFiles" method="getJPGList"
 ReturnVariable ="theList"/>

After the cfinvoke tag, use the variable, theList, which contains the entire directory of JPEGs. Transform this simple component into a web service.  Doing so makes the information accessible to anyone on the Internet using any capable technology.  Simply add access="remote" to the cffunction tag in the CFC. ColdFusion MX takes care of all the details, including creating the WDSL file for you. You've successfully published a web service that lists the contents of one of your directories. This was a simple example.  Yet, compared to that of another language, it illustrates the simplicity and power inherent in ColdFusion code. Read more about using ColdFusion components in the ColdFusion Developer Center.

Summary
ASP is certainly a viable technology to build Web applications, but for ASP programmers who wish to make the change to ColdFusion, the transition can be an easy one. You'll find that applications that once took days can be done in hours, and calls to third-party components can be a thing of the past. CFML is a versatile language that is self-contained, easy-to-learn, and promotes rapid application development.  Also, with the new additions of JSP tag libraries, ColdFusion Components, XML functionality, and web services in ColdFusion MX, ColdFusion is more versatile than ever before.

 


About the author
Tom Muck is co-author of three UltraDev books including the bestseller, Dreamweaver UltraDev4: The Complete Reference.  He is an extensibility expert focused on the integration of Macromedia products with ColdFusion and other languages, applications, and technologies.  Tom has been recognized for this expertise as the 2000 recipient of Macromedia's Best UltraDev Extension Award.  He also authors articles and speaks at conferences on this and related subjects.  As Senior Applications Developer for Integram in Northern Virginia, Tom develops back-end applications for expedited, electronic communications. Tom also runs the Basic-UltraDev site with co-author Ray West and is an advisor for MX inSite magazine.