Creating a ColdFusion Application with the CFAPPLICATION Tag

 by Tom Muck

The Application.cfm file and the CFAPPLICATION tag that is associated with the page are necessary for most ColdFusion applications. For those familiar with ASP, the Application.cfm page is similar in concept to the Global.asa file.

Session and Application variables are enabled in this file. You can't use the variables in your application without first enabling them from this file. Also,timeout values for the Application and Session variables are set.

When a page is requested with a .cfm extension, the ColdFusion server first checks whether an Application.cfm file exists. If it does, the server executes the Application.cfm page first. The Application.cfm page, in many cases, will contain the authentication code for a user. If the user is logged in, the page that was requested will execute; if the user is not logged in, the login form in the Application.cfm page will be displayed.

The CFAPPLICATION tag is declared on the page as follows:

<CFAPPLICATION NAME="myApplication"
 SESSIONMANAGEMENT="Yes"
 CLIENTMANAGEMENT="Yes"
 APPLICATIONTIMEOUT="#CreateTimeSpan(2,0,0,0)#"
 SESSIONTIMEOUT="#CreateTimeSpan(0,0,5,0)#"
 >

The SESSIONMANAGEMENT attribute assumes a Yes or No value. If the value is Yes, then Session variables can be used in the pages of the application. The CreateTimeSpan function is a built-in ColdFusion function that enables you to specify days, hours, minutes, and seconds as a comma-delimited list and have the list automatically converted to a date/time object that can be manipulated by the ColdFusion server.

Also, application-level variables can be defined on this page. These are variables that can be used by all users of the application. These variables are usually locked with a CFLOCK tag, as in the following example:

<CFLOCK SCOPE="Application" TIMEOUT="30" TYPE="Exclusive">
<CFIF Not IsDefined("application.States")>
  <CFQUERY NAME="getStates" DATASOURCE="Orders">
  SELECT States from StateTable
  </CFQUERY>
  <CFSET application.States = ValueList(getStates.States)>
</CFIF>
</CFLOCK>

The example first locks the Application variable from all access. Then, it checks whether the Application variable named "States" has been defined. If it has, nothing is executed, but if it hasn't, a CFQUERY tag is executed. The query returns a list of U.S. States, which is then stored as a list in an Application variable named "States". The access of the variable inside of a CFLOCK ensures that no access is permitted to the variable by any other user while it is being written to by this page. This block of code is executed only once when the page is hit for the first time, and then again after the application times out. Application timeouts are usually set pretty high-several days-whereas Session timeouts are usually set between 5 and 20 minutes.

You can easily define a CFAPPLICATION tag on a page with our Server Behavior. The interface looks like this:


You can define any of the CFAPPLICATION tag attributes from this interface. Remember to save the file as Application.cfm in your directory.

This file is needed by the User Authentication server behaviors, because they use Session variables.

Tom Muck

tommuck@basic-ultradev.com