If you do not use an Application.cfc file, use the Application.cfm page to define application-level settings and functions.

Naming the application

Use the cfapplication tag to specify the application name and define a set of pages as part of the same logical application. Although you can create an application by placing a cfapplication tag with the application name on each page, you normally place the tag in the Application.cfm file; for example:

<cfapplication name="SearchApp">

Note: The value that you set for the name attribute in the cfapplication tag is limited to 64 characters.

Setting the client, application, and session variables options

Use the cfapplication tag to specify client state and persistent variable use, as follows:

  • To use Client scope variables, specify clientManagement=True.
  • To use Session scope variables, specify sessionManagment=True.
    You can also optionally do the following:
  • Set application-specific time-outs for Application and Session scope variables. These settings override the default values set in the ColdFusion Administrator.
  • Specify a storage method for Client scope variables. This setting overrides the method set in the ColdFusion Administrator.
  • Specify not to use cookies on the client browser.
    For more information on configuring these options, see Using Persistent Data and Locking and the CFML Reference.

Defining page processing settings

The cfsetting tag lets you specify page processing attributes that you want to apply to all pages in your application. For more information, see Setting page processing options in Defining the application and its event handlers in Application.cfc.

Setting application default variables and constants

Set default variables and application-level constants on the Application.cfm page. For example, specify the following values:

  • A data source
  • A domain name
  • Style settings, such as fonts or colors
  • Other important application-level variables
    Often, an Application.cfm page uses one or more cfinclude tags to include libraries of commonly used code, such as user-defined functions, that are required on many of the application's pages.

Processing logins

When an application requires a user to log in, you typically place the cflogin tag on the Application.cfm page. For detailed information on security and creating logins, including an Application.cfm page that manages user logins, see Securing Applications

Handling errors

Use the cferror tag on your Application.cfm page to specify application-specific error-handling pages for request, validation, or exception errors, as shown in the following example. This way you include application-specific information, such as contact information or application or version identifiers, in the error message, and you display all error messages in the application in a consistent manner.
For more information on error pages and error handling, see Handling Errors.

Example: an Application.cfm page

The following example shows a sample Application.cfm file that uses several of the techniques typically used in Application.cfm pages. For the sake of simplicity, it does not show login processing; for a login example, see Securing Applications.

<!--- Set application name and enable Client and Session variables. --->
<cfapplication name="Products"
clientmanagement="Yes"
clientstorage="myCompany"
sessionmanagement="Yes">

<!--- Set page processing attributes. --->
<cfsetting showDebugOutput="No">

<!--- Set custom global error handling pages for this application.--->
<cferror type="request"
template="requesterr.cfm"
mailto="admin@company.com">
<cferror type="validation"
template="validationerr.cfm">

<!--- Set the Application variables if they aren't defined. --->
<!--- Initialize local app_is_initialized flag to false. --->
<cfset app_is_initialized = False>
<!--- Get a read-only lock. --->
<cflock scope="application" type="readonly" timeout=10>
<!--- Read init flag and store it in local variable. --->
<cfset app_is_initialized = IsDefined("Application.initialized")>
</cflock>
<!--- Check the local flag. --->
<cfif not app_is_initialized>
<!--- Application variables are not initialized yet.
Get an exclusive lock to write scope. --->
<cflock scope="application" type="exclusive" timeout=10>
<!--- Check the Application scope initialized flag since another request
could have set the variables after this page released the read-only
lock. --->
<cfif not IsDefined("Application.initialized")>
<!--- Do initializations --->
<cfset Application.ReadOnlyData.Company = "MyCompany">
<!--- and so on --->
<!--- Set the Application scope initialization flag. --->
<cfset Application.initialized = "yes">
</cfif>
</cflock>
</cfif>

<!--- Set a Session variable.--->
<cflock timeout="20" scope="Session" type="exclusive">
<cfif not IsDefined("session.pagesHit")>
<cfset session.pagesHit=1>
<cfelse>
<cfset session.pagesHit=session.pagesHit+1>
</cfif>
</cflock>

<!--- Set Application-specific Variables scope variables. --->
<cfset mainpage = "default.cfm">
<cfset current_page = "#cgi.path_info#?#cgi.query_string#">

<!--- Include a file containing user-defined functions called throughout
the application. --->
<cfinclude template="commonfiles/productudfs.cfm">

Reviewing the code

The following table describes the code and its function:

Code

Description

 

<cfapplication name="Products"
clientmanagement="Yes"
clientstorage="myCompany"
sessionmanagement="Yes">

 

Names the application, enables Client and Session scope variables, and sets the client variable store to the myCompany data source.

 

<cfsetting showDebugOutput="No">

 

Ensures that debugging output is not displayed, if the ColdFusion Administrator enables it.

 

<cferror type="request"
template="requesterr.cfm"
mailto="admin@company.com">
<cferror type="validation"
template="validationerr.cfm">

 

Specifies custom error handlers for request and validation errors encountered in the application. Specifies the mailing address for use in the request error handler.

 

<cfset app_is_initialized = False>

 

Sets the Application scope variables, if they are not already set. For a detailed description of the technique used to set the Application scope variables, see Using Persistent Data and Locking.

 

<cflock timeout="20" scope="Session" type="exclusive">
<cfif not IsDefined("session.pagesHit")>
<cfset session.pagesHit=1>
<cfelse>
<cfset session.pagesHit=session.pagesHit+1>
</cfif>
</cflock>

 

Sets the Session scope pagesHit variable, which counts the number of pages touched in this session. If the variable does not exist, creates it; otherwise, increments it.

 

<cfset mainpage = "default.cfm">
<cfset current_page = "#cgi.path_info#?#cgi.query_string#">

 

Sets two Variables scope variables that are used throughout the application. Creates the current_page variable dynamically; its value varies from request to request.

 

<cfinclude template="commonfiles/productudfs.cfm">

 

Includes a library of user-defined functions that are used in most pages in the application.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy