Checking To See If CFFlush Has Already Been Executed

Posted October 4, 2006 at 4:53 PM by Ben Nadel

Tags: ColdFusion

I just posted this on the House of Fusion CF-Talk list, but I thought I would posted up here. Someone has asked if there was a way to determine if the CFFlush tag had already been executed. I am sure there is a way to get this by digging around in the GetPageContext() family of objects, but you can perform this simply by trying to set a header value:

  • <cftry>
  • <!--- Try set a status code. --->
  • <cfheader
  • statuscode="200"
  • statustext="OK"
  • />
  •  
  • <!---
  • We set the status code, flush has
  • not been called.
  • --->
  • <cfset REQUEST.FlushInitiated = false />
  •  
  • <cfcatch>
  •  
  • <!---
  • The CFHeader tag has thrown an error. CFFlush
  • has already been executed.
  • --->
  • <cfset REQUEST.FlushInitiated = true />
  •  
  • </cfcatch>
  • </cftry>

CFHeader tags can only be executes prior to content being sent to the browser since header information is the first thing that is sent. If content has already been flushed to the browser, then CFHeader will throw an error upon execution. In this example, we CFTry to execute a CFHeader tag. If it works, then no content has been sent to the browser, and we set our flag (REQUEST.FlushInitiated) accordingly. If an error is thrown by the CFHeader tag, then we know CFFlush has been executed and we set our flag accordingly.

If you want to do this with out having to set a variable, the easy solution would be to just wrap it in a ColdFusion user defined function (UDF):

  • <cffunction
  • name="IsFlushInitiated"
  • access="public"
  • returntype="boolean"
  • output="false"
  • hint="Determines if a CFFlush tag has been executed yet.">
  •  
  • <cftry>
  • <!--- Try set a status code. --->
  • <cfheader
  • statuscode="200"
  • statustext="OK"
  • />
  •  
  • <!---
  • We set the status code, flush has not
  • been called.
  • --->
  • <cfreturn false />
  •  
  • <cfcatch>
  •  
  • <!---
  • The CFHeader tag has thrown an error. CFFlush
  • has already been executed.
  • --->
  • <cfreturn true />
  •  
  • </cfcatch>
  • </cftry>
  • </cffunction>

Then you can just call this like:

  • <!--- Check to see if CFFlush has been called. --->
  • <cfif IsFlushInitiated()>
  • ....
  • </cfif>


Reader Comments

Mar 13, 2007 at 1:03 PM // reply »
317 Comments

Just added this to ColdFire. Thanks!


Mar 13, 2007 at 1:18 PM // reply »
10,743 Comments

Cool :)


May 1, 2007 at 4:00 PM // reply »
19 Comments

Throwing an exception as regular program flow is far from best practice and can also be quite expensive in some situations. This method also has the unintended side effect that checking if the buffer has been flushed resets the response code to 200 which may be undesirable if a different part of the application had explicitly set the response code to something else previously.

You're right though, there is a PageContext method! :)

getPageContext().getResponse().isCommitted()

ref: http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletResponse.html#isCommitted()

On a side note. If for some reason you *cannot* use getPageContext(), for instance if you're on CF5, an alternative method would be:

<cfheader name="CFFlushInited" value="false">

which will throw an exception just like your code, but won't overwrite potentially valuable response information.


May 1, 2007 at 4:06 PM // reply »
10,743 Comments

@Elliott,

Good point; throwing exceptions is not a good practice. But to be fair, checking to see if CFFlush has been executed is probably not a best practice either :)

Thanks for pointing out a better way to perform this task. I think I saw something like this in FireBug or something (Ray Camden).

As far as an alternate method for CF5... what is CF5? ;)

Silly people still using CF5....


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2012 at 1:58 AM
Updated: Converting A ColdFusion Query To CSV Using QueryToCSV()
Hi Ben, why do you need to have so many double quotes when adding the field and field name to the row data? ----------------------------------------- <cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = ... read »
AXL
May 21, 2012 at 1:24 AM
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
@Mounir, Open your lower case URL Rewrite rule and add the following condition. Condition input: {REQUEST_URI} Check if input string: Does Not Match the Pattern Pattern: ^/CFFileServlet/_cf_ca ... read »
May 20, 2012 at 4:28 AM
Understanding The Complex And Circular Relationships Between Objects In JavaScript
@Will Vaughn I tried your javascript example but got this error:- foo.print is not a function ... read »
May 19, 2012 at 5:37 AM
A Graphical Explanation Of Javascript Closures In A jQuery Context
Thanks for this article, but I fear you missed an important point. If variables in the outer context change, these changes affect the inner anonymous functions as well. That means: if you change the ... read »
May 18, 2012 at 3:39 PM
Parsing CSV Data With An Input Stream And A Finite State Machine
Can you use file upload button with this? and read live? or does the file have to already be on the server saved? ... read »
May 18, 2012 at 1:06 AM
VIRGO (Aug. 23-Sept. 22): Dead On The Money!
A friend of mine and I were arguing about astrology and she told me that he believes in astrology. She hasn't provided me with any evidence that the belief makes any sense to me. She she been telling ... read »
May 17, 2012 at 11:32 PM
Using ColdFusion to Handle 404 Errors (Page Not Found) On Development Server
Very easy the configuration. I read a lot pages and I can't find the solution. I open the administrator and change this Administrator/server settings/Error Handlers/Missing Template Handler and p ... read »
May 17, 2012 at 3:13 PM
LOCAL Variables Scope Conflicts With ColdFusion Query of Queries
I never cease to be amazed that almost EVERY random CF issue I come across lands me on your site. Thank you for documenting your findings for the world. ... read »