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 »
10,640 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 »
19 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 »
10,640 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 »
10,640 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »