Server-Side Redirections in CFMX

Submitted by Charlie Arehart,

Did you know that you can now do server-side redirection (a forward) in ColdFusion MX? It's not being mentioned much, and some might not appreciate what doors this opens, but in the right circumstances, it's a big improvement over our prior redirection via CFLOCATION. Though many didn't know it, CFLOCATION did a client-side redirect, meaning a header was sent to the browser to have it request the new URL. One of the big challenges/ drawbacks of this was that no variables set in the caller were available to the called program.

Server-side redirects or forwards, as they're known in Servlets and JSP do allow you to share REQUEST scope variables between templates (between ColdFusion and ColdFusion or between ColdFusion and JSP). They also are the key to doing true model-view-controller style design. You can now do it using ColdFusion MX's capability to use the new getPageContext function, which exposes the methods in the underlying Servlet PageContext object. The key for this solution is to use the forward method, as follows:

 <CFSCRIPT> getPageContext().forward("sometemplate"); </ CFSCRIPT> 

Again, this does a server-side transfer of control (much like the ASP3 server.transfer did versus the older response.redirect), and it allows any variables set in the request scope of the caller to be available in the called program (via the request scope there). There are some things to consider. The URL shown to the user will still be that of the page that did the forward, not the page to which control was redirected. This has several ramifications: challenges if they bookmark it, possibly unexpected results for you or them if they click the Refresh button. These issues are well discussed in the literature regarding the use of forwards in JSPs and Servlets. Over time, we'll all come to learn the same lessons and find the strengths and weaknesses of this approach.

One last bonus: Even though it's a server-side redirect and distinctly unlike CFLOCATION, any cookies you set before the forward are indeed passed to the browser.

Inside ColdFusion MX
Inside Coldfusion MX
ISBN: 0735713049
EAN: 2147483647
Year: 2005
Pages: 579 © 2008-2017.
If you may any questions please contact us: