DevLearningTools

MODULE 8 · LESSON 02

Application Lifecycle

The full, fixed order ColdFusion fires Application.cfc's event methods in — from the server starting through every request, session, and shutdown — plus onRequestEnd, onAbort, onCFCRequest, and the onServerStart method that isn't actually part of Application.cfc.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

The previous lesson covered onApplicationStart, onRequestStart/onRequest, and onError individually. This lesson is about the bigger picture: the fixed order all of Application.cfc's event methods fire in, plus the four events that lesson didn't cover — onRequestEnd, onAbort, onCFCRequest, and onServerStart.

Learning Objectives

After completing this lesson, you'll be able to:

  • Name all the Application.cfc lifecycle events in the order they fire.
  • Write an onRequestEnd method for cleanup or footer-style logic.
  • Explain when onAbort runs instead of onRequestEnd.
  • Explain why onServerStart isn't actually a method of Application.cfc.

The Full Event List

MethodFires when
onServerStartThe ColdFusion server itself starts (lives in Server.cfc, not Application.cfc)
onApplicationStartThe application's very first request
onSessionStartA new session begins
onRequestStartThe beginning of every request
onCFCRequestAn HTTP/AMF call targets a CFC directly (Ajax, web service, Flash Remoting), instead of onRequest
onRequestRight after onRequestStart, for a normal .cfm page request
onRequestEndAfter every other CFML code in the request has finished
onAbortInstead of onRequestEnd, if the request used <cfabort>, <cflocation>, or <cfcontent>
onErrorAny uncaught exception, at any point in the above
onSessionEndA session times out from inactivity
onApplicationEndThe application times out, or the server shuts down
onMissingTemplateInstead of onRequestStart/onRequest, when the requested .cfm page doesn't exist

The Normal Request Flow

onApplicationStart (first request only)
onSessionStart (new session only)
onRequestStart
onRequest (.cfm page)onCFCRequest (CFC/Ajax call)
onRequest
onRequestEnd
NOTE

onApplicationStart and onSessionStart only fire under their specific conditions (first-ever request, new session) — every other request skips straight to onRequestStart.

onRequestEnd — Cleanup After Everything Else Runs

onRequestEnd fires after all the page's own CFML code has finished, but while the page context is still available — useful for performance logging or dynamic footer content.

CFScript
<cfscript>

function onRequestEnd(targetPage) {
    if (structKeyExists(session, "username")) {
        include "authuserfooter.cfm";
    } else {
        include "noauthuserfooter.cfm";
    }
}

</cfscript>
Tag Syntax
<cffunction name="onRequestEnd" returnType="void">
    <cfargument name="targetPage" type="string" required="true">
    <cfif structKeyExists(session, "username")>
        <cfinclude template="authuserfooter.cfm">
    <cfelse>
        <cfinclude template="noauthuserfooter.cfm">
    </cfif>
</cffunction>
NOTE

onRequestEnd never runs at all if the request used <cfabort>, <cflocation>, or <cfcontent> — onAbort takes over instead in that case.

onAbort — When a Request Gets Cut Short

CFScript
<cfscript>

function onAbort(targetPage) {
    writeLog(file = "myapp", type = "information", text = "Request aborted: " & targetPage);
}

</cfscript>
Tag Syntax
<cffunction name="onAbort" returnType="void">
    <cfargument name="targetPage" type="string" required="true">
    <cflog file="myapp" type="information" text="Request aborted: #arguments.targetPage#">
</cffunction>
NOTE

This isn't just about a literal <cfabort> call — <cflocation> and <cfcontent> also trigger onAbort instead of onRequestEnd, since both of those also end the response early.

onCFCRequest — Direct CFC Calls (Ajax, Web Services)

When a CFC is called directly over HTTP (rather than through a normal .cfm page), onCFCRequest fires instead of onRequestStart/onRequest — useful for centralizing logging or validation across every CFC endpoint.

CFScript
<cfscript>

function onCFCRequest(cfcname, method, args) {
    writeLog(file = "myapp", type = "information", text = "CFC call: " & cfcname & "." & method);
    return invoke(cfcname, method, args);
}

</cfscript>
Tag Syntax
<cffunction name="onCFCRequest" returnType="void">
    <cfargument name="cfcname" type="string" required="true">
    <cfargument name="method" type="string" required="true">
    <cfargument name="args" type="struct" required="true">
    <cflog file="myapp" type="information" text="CFC call: #arguments.cfcname#.#arguments.method#">
    <cfinvoke component="#arguments.cfcname#" method="#arguments.method#" returnVariable="result" argumentCollection="#arguments.args#">
</cffunction>

onServerStart Isn't Actually Part of Application.cfc

Despite being listed alongside the other lifecycle events, onServerStart doesn't live in Application.cfc at all — it lives in a separate file, Server.cfc, in the web root (or a custom path set in the ColdFusion Administrator). It's disabled by default and has to be turned on explicitly.

It also behaves differently: it takes no arguments, is the only method in Server.cfc, runs once when the ColdFusion server itself starts (independent of any specific application), and can't access the Application or Request scopes — there isn't one yet at that point.

Common Beginner Mistakes

Expecting onRequestEnd to run after a <cflocation> redirect

It doesn't — <cflocation> (along with <cfabort> and <cfcontent>) triggers onAbort instead. Put cleanup logic that must always run in both places, or restructure to avoid relying on onRequestEnd for anything critical.

Trying to add onServerStart directly to Application.cfc

It has no effect there — onServerStart only works inside a separate Server.cfc file, and has to be explicitly enabled in the ColdFusion Administrator first.

Assuming onCFCRequest fires for every CFC method call

It only fires for CFCs invoked directly over HTTP/AMF (Ajax, web services, Flash Remoting) — a CFC instantiated and called normally from within a .cfm page doesn't go through onCFCRequest at all.

Best Practices

  • Don't put logic in onRequestEnd that absolutely must run even when a page redirects or aborts — use onAbort for that, or a pattern that doesn't depend on either.
  • Keep onCFCRequest lightweight (logging, basic validation) — it sits in front of every direct CFC call, so slow code there slows down all of them.
  • Only enable Server.cfc's onServerStart for genuinely application-independent setup (scheduler configuration, logging setup) — not application-specific logic.

Interview Questions

What replaces onRequestEnd when a request uses <cfabort>, <cflocation>, or <cfcontent>?

onAbort runs instead of onRequestEnd in all three of those cases.

Where does onServerStart actually live?

In a separate Server.cfc file, not Application.cfc — despite commonly being listed alongside the other lifecycle events, it isn't one of them.

What's the difference between onRequest and onCFCRequest?

onRequest handles normal .cfm page requests and must explicitly include the page. onCFCRequest handles CFCs called directly over HTTP/AMF (Ajax, web services, Flash Remoting) instead.

Summary

In this lesson, you covered the full, fixed order of Application.cfc's lifecycle events, wrote onRequestEnd and onAbort methods, covered onCFCRequest for direct CFC calls, and learned that onServerStart actually belongs to a separate Server.cfc file.

What's Next?

The next page is a dedicated interview-prep companion for everything covered in this lesson and the last one — the specific questions people actually get asked about Application.cfc, each with a real-world analogy.