Application.cfm and Application.cfc

Learn how ColdFusion defines an application, manages lifecycle events, initializes shared state, and wraps every request.

Application.cfm: The Classic Model MX 6+

ColdFusion searches upward from the requested template for an Application.cfm file. That file runs at the beginning of each request in the application.

<cfapplication
    name="LegacySupportPortal"
    sessionmanagement="yes"
    sessiontimeout="#CreateTimeSpan(0, 0, 30, 0)#"
    applicationtimeout="#CreateTimeSpan(1, 0, 0, 0)#"
    setclientcookies="yes">

<cfset request.dsn = "support_portal">
<cfset request.applicationRoot = ExpandPath(".")>

Older applications often put configuration, security checks, and reusable initialization directly in Application.cfm. Because it runs for every request, expensive work should not be repeated there unnecessarily.

OnRequestEnd.cfm

A neighboring OnRequestEnd.cfm file can run after the requested template. It was sometimes used for closing layout markup, timing, or cleanup.

<!--- OnRequestEnd.cfm --->
<cfif StructKeyExists(request, "requestStartedAt")>
    <cfset variables.elapsedMs = GetTickCount() - request.requestStartedAt>
    <cflog file="request-time" text="#cgi.script_name# completed in #variables.elapsedMs# ms">
</cfif>

Application.cfc MX 7+

MX 7 introduced a structured application component with lifecycle methods. In MX 7, write it with tag-based CFC syntax.

<cfcomponent output="false">

    <cfset this.name = "LegacySupportPortal">
    <cfset this.sessionManagement = true>
    <cfset this.sessionTimeout = CreateTimeSpan(0, 0, 30, 0)>
    <cfset this.applicationTimeout = CreateTimeSpan(1, 0, 0, 0)>
    <cfset this.scriptProtect = "all">

    <cffunction name="onApplicationStart" returntype="boolean" output="false">
        <cflock scope="application" type="exclusive" timeout="10">
            <cfset application.dsn = "support_portal">
            <cfset application.supportEmail = "[email protected]">
        </cflock>
        <cfreturn true>
    </cffunction>

    <cffunction name="onSessionStart" returntype="void" output="false">
        <cfset session.isAuthenticated = false>
        <cfset session.userId = 0>
    </cffunction>

    <cffunction name="onRequestStart" returntype="boolean" output="false">
        <cfargument name="targetPage" type="string" required="true">
        <cfset request.requestStartedAt = GetTickCount()>
        <cfreturn true>
    </cffunction>

    <cffunction name="onError" returntype="void" output="true">
        <cfargument name="exception" required="true">
        <cfargument name="eventName" type="string" required="true">

        <cflog file="application" type="error"
            text="#arguments.eventName#: #arguments.exception.message#">

        <h1>Application Error</h1>
        <p>The problem has been logged.</p>
    </cffunction>

</cfcomponent>

Common Lifecycle Methods

MethodPurpose
onApplicationStartInitialize shared application configuration and caches.
onApplicationEndPerform final application cleanup when the application expires.
onSessionStartInitialize a visitor's session state.
onSessionEndHandle session expiration; request-specific scopes are not available in the normal way.
onRequestStartValidate or prepare each request before the target page runs.
onRequestTake control of including the requested template.
onRequestEndRun logic after request processing.
onErrorCentralize application exception handling.
onMissingTemplateHandle a missing requested template.

Configuration Placement

Application Scope

Use for shared, read-mostly values such as a datasource name, feature flags, or a cached lookup table. Protect mutations with appropriate locking.

Request Scope

Use for values needed across templates during one request: service objects, user context, timing data, or request-specific configuration.

Component Variables

Use for dependencies and internal state belonging to a CFC instance, such as variables.dsn.

Never in Web-Readable Files

Do not place plaintext production secrets in downloadable backups, source repositories, comments, or files that the web server might serve directly.

Choosing Between the Two

Use Application.cfm when reproducing an MX 6/6.1 application or documenting what a legacy codebase already does. Use tag-based Application.cfc for an MX 7-focused learning project because its lifecycle is easier to reason about.