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

Posted November 3, 2006 at 8:33 AM

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

 Launch code in new window » Download code as text file »

  • <!--- 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

 Launch code in new window » Download code as text file »

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

Download Code Snippet ZIP File

Comments (7)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader 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.

:-<

Posted by Peter Bell on Nov 3, 2006 at 1:36 PM


Pete,

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

Posted by Ben Nadel on Nov 3, 2006 at 2:49 PM


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

Posted by Patrick McElhaney on Nov 6, 2006 at 4:19 PM


Patrick,

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

Thanks for the link as well.

Posted by Ben Nadel on Nov 6, 2006 at 5:28 PM


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

Posted by dickbob on Nov 11, 2006 at 4:15 AM


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).

Posted by Ben Nadel on Nov 11, 2006 at 3:37 PM


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

Posted by dickbob on Nov 11, 2006 at 6:13 PM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting