Is ColdFusion's GetPageContext().Forward() Method Worth Using?

Posted November 3, 2006 at 8:33 AM by Ben Nadel

Tags: ColdFusion

My traditional form-based-page model is such that a form submits back to itself for data validation and manipulation. If all goes well, I perform a CFLocation tag to send the user to either a confirmation page or some other type of "success" page. This works great. The only issue is that it involves going back to the browser. CFLocation sends a 302 response code back to the browser requesting that the browser then redirect to the given Url. This is a tiny interaction, bit if you are looking for extreme optimization, this can be slow.

The beauty of the Forward() method in ColdFusion's page context (via GetPageContext()) is that it stops the current page execution and "forwards" the current response to another part of the application (ie. the url you pass in). It doesn't have to go back to the browser at all. At first, this seems really snazzy and upon testing appears to be a fraction of a second faster. But, is this fraction of a second in performance worth the baggage that comes with it?

I guess it's a philosophical argument. I say NO, that it is not worth it. These are my reasons:

  1. Once on the success page, there is no visible URL change for the user / browser.
  2. Once on the success page, if the user were to refresh the page, the browser would try to resubmit the FORM data as it thinks you are still on the same page.

To me, these things are just not worth it. But again, this is philosophical. I am sure there are plenty of people that would argue that when a user refreshes a form confirmation page, the browser SHOULD resubmit data. They might also argue that "success" page shouldn't be a different url, that it is merely a different "phase" of the current form. Oh well, to each his own. At the very least, it was nice to experiment with.

Index.cfm Form Page

  • <!--- Kill extra output. --->
  • <cfsilent>
  •  
  • <!--- Param form methods. --->
  • <cfparam name="FORM.submitted" type="numeric" default="0" />
  • <cfparam name="FORM.forward_type" type="string" default="forward" />
  • <cfparam name="FORM.name" type="string" default="" />
  • <cfparam name="FORM.hotness" type="string" default="" />
  •  
  •  
  • <!--- Check to see if the form has been submitted. --->
  • <cfif FORM.submitted>
  •  
  • <!--- Set up URL for forwarding. --->
  • <cfset strUrl = "./confirm.cfm?name=#FORM.name#&hotness=#FORM.hotness#" />
  •  
  • <!--- Forward to success page. --->
  • <cfif NOT Compare( FORM.forward_type, "forward" )>
  • <cfset GetPageContext().Forward( strUrl ) />
  • <cfelse>
  • <cflocation url="#strUrl#" addtoken="false" />
  • </cfif>
  •  
  • </cfif>
  •  
  • </cfsilent>
  •  
  • <cfoutput>
  •  
  • <h2>
  • Page One : Index.cfm
  • </h2>
  •  
  • <form action="#CGI.script_name#" method="POST">
  • <input type="hidden" name="submitted" value="1" />
  •  
  • <label>
  • Name:<br />
  • <input type="text" name="name" value="#FORM.name#" size="30" /><br />
  • </label>
  • <br />
  •  
  • <label>
  • Hotness:<br />
  • <input type="text" name="hotness" value="#FORM.hotness#" size="10" /><br />
  • </label>
  • <br />
  •  
  • <label>
  • <input type="radio" name="forward_type" value="cflocation"
  • <cfif (FORM.forward_type EQ "cflocation")>checked="true"</cfif>
  • />
  • CFLocation
  • </label>
  •  
  • &nbsp;&nbsp;
  •  
  • <label>
  • <input type="radio" name="forward_type" value="forward"
  • <cfif (FORM.forward_type EQ "forward")>checked="true"</cfif>
  • />
  • Forward()
  • </label>
  • <br />
  • <br />
  •  
  • <input type="submit" value="Submit" /><br />
  •  
  • </form>
  •  
  • </cfoutput>

Confirm.cfm Form Confirmation Page

  • <cfoutput>
  •  
  • <h2>
  • Page Two : Confirm.cfm
  • </h2>
  •  
  • <p>
  • I agree, #URL.name# was a definate #URL.hotness#!
  • </p>
  •  
  • </cfoutput>


Reader Comments

Nov 3, 2006 at 1:36 PM // reply »
111 Comments

Hi Ben,

Point 2 is the killer. My old procedural controller was queue based. It would undertake an action within a while loop until the queue was empty, so all you needed to do to go from form processing to list display was to add an If FormValid THEN <Add UserList to controller queue>.

It was great until I realized that refreshing the list added another new instance if you'd just filled out an add form.

:-<


Nov 3, 2006 at 2:49 PM // reply »
11,238 Comments

Pete,

Exactly. That's why I think for me, this is not going to be a solution that I can really use.


Nov 6, 2006 at 4:19 PM // reply »
26 Comments

Good point, Ben. I also find the confirm page is easier to develop and debug when I can call it directly from the browser.

The only advantage to server-side redirects I've found is the ability to see debugging information in the browser after you submit the form. I found a way around that problem with a custom tag.

http://pmcelhaney.weblogs.us/archives/cf-debug-tip


Nov 6, 2006 at 5:28 PM // reply »
11,238 Comments

Patrick,

True, the page is MUCH easier to debug when you can call it directly from a browser.

Thanks for the link as well.


Nov 11, 2006 at 4:15 AM // reply »
4 Comments

I too like to use self-posting forms. My issue is that after a sucessful validation and the following cflocation the form scope is lost for the confirmation page. How do you get around that?

dickbob


Nov 11, 2006 at 3:37 PM // reply »
11,238 Comments

DB,

Most of the time I do not need the form data on the confirmation page. However, if I do need some of the information, I generally pass it through the URL or in a uniquely key'd session object (and then just pass the unique key in the url).


Nov 11, 2006 at 6:13 PM // reply »
4 Comments

Yes, I too put the form data into a session object to get it to persist through to the cflocation page. I tend to use a structure name based on the form id but using a unique id that is passed through in the URL is an idea I'll have to consider.

Thanks for sharing your technique.

dickbob


Jan 12, 2011 at 5:04 AM // reply »
3 Comments

Hi Ben,

We had just an issue on one of our CF7 Hosting Servers where the server was getting out of memory and the logs showed a template that was using the getPageContext().forward() method.

According to the following technote, after the forward method a cfabort should be used or at least no output should be generated.

http://kb2.adobe.com/cps/401/kb401382.html

Don't know if later Versions of CF are affected too.


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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools