Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

New Possibility in CFMX: Server-Side Redirects

New Possibility in CFMX: Server-Side Redirects

Now that we can talk freely about ColdFusion MX, I want to share something new that you likely won't hear much about...

...but that I think stands to be an important feature: server-side redirects, or "forwards." It's something CF has long been missing, and an improvement over CFLOCATION. The thing about a forward is that it's quite different from, and in most ways much better than, a CFLOCATION. We've all used CFLOCATION to transfer control from one CF page to another. But did you know that it performs a client-side redirection? The user doesn't see it happen, and maybe you never noticed it. It's done by CF sending a special HTTP header to the browser. Maybe it never mattered to you, but this behavior has some negative implications.

The most important one is that data that's passed into the calling page isn't available to the called page. In other words, if template1.cfm is called as a form action page and is passed several form variables, when you do some processing and then use CFLOCATION to transfer to template2.cfm, you won't have access to those form variables in template2.cfm (see Figure 1).

Many have tried to get around this challenge by passing the form variables on the query string in the CFLOCATION's URL, or by setting the variables into the session scope. Still another approach is to CFINCLUDE template2.cfm instead of using CFLOCATION to call it, or perhaps call it as a custom tag. Each has its place. But there may be times when for modularity, encapsulation, or other purposes, you do indeed want to transfer control to the next program, but also pass along all the data that was sent to the caller. Further, you may want to pass along any data created in the calling page (template1.cfm in our example), such as queries that were executed or the result of any other operations (such as a CFHTTP, CFLDAP, etc.). Again, this data won't be made available to the called page with a CFLOCATION. But it can be made available in a server-side redirect.

The Power of Server-Side Redirects
Server-side redirects solve this problem. Data in the calling page can be made available to the called page. ASP added this feature with their server.transfer() method (whereas their response.redirect() acted like our CFLOCATION). And servlets and JSPs have long had the concept of a "forward."

Indeed, because CFMX is based on an underlying servlet/JSP engine, we now have exposed to us the same capability. To do it, we simply leverage a new function in CFMX, which is in fact exposing to us the servlet PageContext that allows servlet and JSP developers to use a forward. In fact, the syntax to do so may look a lot alike, but it's all CFML:

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

This doesn't have to be done in a CFSCRIPT, but it works a little more easily this way. Still, for those who don't want to remember all this detail, I've created a simple custom tag called CF_FORWARD that can often be used in place of CFLOCATION. You can find it in the Macromedia Developer Exchange, at http://devex.macromedia.com/developer/gallery/info.cfm? ID=C0DB78BB-617C-11D6-840300508B94F380& method=Full.

An example of using it might be:

<CF_FORWARD URL="relativeURL">

Note that in both examples the relative URL can be any CF template (and even a JSP template if it's been placed within the CFMX directories where your CF templates are located). Note also that you can pass a query string on the URL, just as you can in CFLOCATION.

Sharing Data Between Pages
I said earlier that one of the key new features is that you can share data between the calling and called pages. The only trick is that the variables to be shared must be placed in the REQUEST scope. This scope has been available to us in CF for a while and was used previously for sharing data with custom tags.

Now, if you place some variables into the request scope, or perhaps create a query on the calling page with NAME="request.queryname", then those same REQUEST variables will be available (using the same REQUEST scope and variable names) in the called page. In fact, one feature I added to my custom tag is that if you use the LOCALVARSCOPIED="yes" attribute, it will automatically copy local variables (such as queries created on the page with no specific scope or the VARIABLES scope) into the REQUEST scope before calling the forward.

Note, too, that this means that CF and JSPs/servlets can also transfer control between each other and access each other's REQUEST scopes (although the REQUEST scope in JSPs and servlets means a bit more than it does in CFMX).

This ability to do server-side redirects is also the key to doing true Model-View-Controller (MVC) style design of your applications. That's a subject beyond the scope of this article, but for those who understand it, it opens very interesting doors to us. I hope more people in the CF world will investigate this possibility now that it's closer to a reality.

Not Perfect
Sadly, the forward method as currently implemented (in the preview release candidate, or RC) isn't perfect for a couple of reasons.

First, and perhaps challenging to those with servlet/JSP experience, I've found that unlike JSPs and servlets (for those familiar with them), in CFMX the REQUEST scope of the caller doesn't automatically include the FORM and URL variables that might have been passed to it. The good news, however, is that any URL variables available on the calling page are also available on the called page. In this respect it works like custom tags.

The second problem is more serious. I've found (again, in the RC) that if I try to do a forward on a page (or within the custom tag called from a page) that is itself a form action page (has "form." variables available), the forward fails with an "err.io.short_read" error. That's strange and a real bummer. Until that's resolved, you won't be able to forward from a form action page. I've got the custom tag detecting and stopping before that error.

This is real tragic, though, for using MVC style design for form action pages. I hope it's either fixed in a later RC or can be fixed before the final production release.

A Bright Spot
Despite the bad news, there's a piece of really good news. Many have surely been hassled by the fact that if you set a cookie before doing a CFLOCATION, it never gets set on the client. But I've found that if you set a cookie before doing a forward, it is indeed set to the client. It seems like magic, but JRun's documentation suggests that the JSP:forward tag has always had this behavior. So it's just another benefit we leverage since CF is based on the underlying JSP/servlet engine.

Other Considerations
Finally, there are some things to be aware of with using a forward. When you do a server-side redirect, since by design it doesn't send a redirect through the browser, the URL shown to the user will still be that of the page they requested that did the forward, not the page to which control was redirected. In our example above, the URL will show template1.cfm. This has several ramifications (challenges if they bookmark it, possibly unexpected results for you or them if they hit the refresh button).

These are issues well discussed in the literature regarding the use of forward in JSPs and servlets, and I imagine ASPs since the introduction of server.transfer(). Over time, we'll all come to learn the same lessons and find the strengths and weaknesses of this approach.

It may also be interesting to see if the Fusebox community adopts this new approach in their design, given that it already attempts the kind of encapsulation and decoupling that is a hallmark of MVC design.

Of course, all this shouldn't challenge the Macromedia mantra that in CFMX one need not learn Java. No, one need not. I'm just pointing out that doors are opening that may offer interesting rewards for those who explore them - and perhaps eventually those discoveries may trickle into more abstractions for the CFMX masses.

More Stories By Charlie Arehart

A veteran ColdFusion developer since 1997, Charlie Arehart is a long-time contributor to the community and a recognized Adobe Community Expert. He's a certified Advanced CF Developer and Instructor for CF 4/5/6/7 and served as tech editor of CFDJ until 2003. Now an independent contractor (carehart.org) living in Alpharetta, GA, Charlie provides high-level troubleshooting/tuning assistance and training/mentoring for CF teams. He helps run the Online ColdFusion Meetup (coldfusionmeetup.com, an online CF user group), is a contributor to the CF8 WACK books by Ben Forta, and is frequently invited to speak at developer conferences and user groups worldwide.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Patrick Correia 05/07/03 03:40:00 PM EDT

According to the release notes for CFMX Updater 3, Macromedia claims this problem has been fixed.

charles arehart 06/26/02 10:01:00 AM EDT

Folks, just wanted to share a couple updates about the article. I had mentioned there were two "problems" in the release candidate of CFMX, which was the latest version when I wrote the article. Since then, the final version has come out, but the "problems" remain.

Actually, there is only one bug. The other is just something to understand.

The bug is that if you try to do a forward from a page that's a FORM action page, it fails with an error "err.io.short_read". That is a recognized (and very unfortunate) bug, that perhaps we can hope will be fixed in some sort of patch or service pack.

The other problem I'd mentioned, that FORM and URL variables weren't put in the REQUEST scope, is just something to understand. It makes sense in CF's design that this is not the case. The REQUEST scope isn't written to automatically at all. It's only for you to put things into it. But there is good news that mitigates this somewhat.

Let me first clarify that the only reason one would expect this would be from experience with either ASP or JSP, which put form posts and query strings into a "request" scope of sorts.

The good news is that, usually, any URL vars that exist on the calling page will be available on the page to which you forward. So it's not necessary to put them in the REQUEST scope to be able to access them (of course, with FORM vars not working at all, I can't vouch that this should work for them, but it's reasonable to expect).

But I've noticed one quirk: if the URL used in the forward has a query string specified, then that seems to cancel out the availability of the URL vars that were available on the calling page. It's as if passing any on the forward URL blocks any that had been passed to the forwarding page.

Oh well. Maybe that will be observed and resolved as a bug.

/charlie

@ThingsExpo Stories
High-velocity engineering teams are applying not only continuous delivery processes, but also lessons in experimentation from established leaders like Amazon, Netflix, and Facebook. These companies have made experimentation a foundation for their release processes, allowing them to try out major feature releases and redesigns within smaller groups before making them broadly available. In his session at 21st Cloud Expo, Brian Lucas, Senior Staff Engineer at Optimizely, will discuss how by using...
In this strange new world where more and more power is drawn from business technology, companies are effectively straddling two paths on the road to innovation and transformation into digital enterprises. The first path is the heritage trail – with “legacy” technology forming the background. Here, extant technologies are transformed by core IT teams to provide more API-driven approaches. Legacy systems can restrict companies that are transitioning into digital enterprises. To truly become a lead...
SYS-CON Events announced today that Daiya Industry will exhibit at the Japanese Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ruby Development Inc. builds new services in short period of time and provides a continuous support of those services based on Ruby on Rails. For more information, please visit https://github.com/RubyDevInc.
As businesses evolve, they need technology that is simple to help them succeed today and flexible enough to help them build for tomorrow. Chrome is fit for the workplace of the future — providing a secure, consistent user experience across a range of devices that can be used anywhere. In her session at 21st Cloud Expo, Vidya Nagarajan, a Senior Product Manager at Google, will take a look at various options as to how ChromeOS can be leveraged to interact with people on the devices, and formats th...
SYS-CON Events announced today that Yuasa System will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Yuasa System is introducing a multi-purpose endurance testing system for flexible displays, OLED devices, flexible substrates, flat cables, and films in smartphones, wearables, automobiles, and healthcare.
SYS-CON Events announced today that Taica will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Taica manufacturers Alpha-GEL brand silicone components and materials, which maintain outstanding performance over a wide temperature range -40C to +200C. For more information, visit http://www.taica.co.jp/english/.
As hybrid cloud becomes the de-facto standard mode of operation for most enterprises, new challenges arise on how to efficiently and economically share data across environments. In his session at 21st Cloud Expo, Dr. Allon Cohen, VP of Product at Elastifile, will explore new techniques and best practices that help enterprise IT benefit from the advantages of hybrid cloud environments by enabling data availability for both legacy enterprise and cloud-native mission critical applications. By rev...
Organizations do not need a Big Data strategy; they need a business strategy that incorporates Big Data. Most organizations lack a road map for using Big Data to optimize key business processes, deliver a differentiated customer experience, or uncover new business opportunities. They do not understand what’s possible with respect to integrating Big Data into the business model.
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, will discuss how they b...
Enterprises have taken advantage of IoT to achieve important revenue and cost advantages. What is less apparent is how incumbent enterprises operating at scale have, following success with IoT, built analytic, operations management and software development capabilities – ranging from autonomous vehicles to manageable robotics installations. They have embraced these capabilities as if they were Silicon Valley startups. As a result, many firms employ new business models that place enormous impor...
SYS-CON Events announced today that Dasher Technologies will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Dasher Technologies, Inc. ® is a premier IT solution provider that delivers expert technical resources along with trusted account executives to architect and deliver complete IT solutions and services to help our clients execute their goals, plans and objectives. Since 1999, we'v...
SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
SYS-CON Events announced today that TidalScale, a leading provider of systems and services, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale has been involved in shaping the computing landscape. They've designed, developed and deployed some of the most important and successful systems and services in the history of the computing industry - internet, Ethernet, operating s...
SYS-CON Events announced today that TidalScale will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale is the leading provider of Software-Defined Servers that bring flexibility to modern data centers by right-sizing servers on the fly to fit any data set or workload. TidalScale’s award-winning inverse hypervisor technology combines multiple commodity servers (including their ass...
Amazon is pursuing new markets and disrupting industries at an incredible pace. Almost every industry seems to be in its crosshairs. Companies and industries that once thought they were safe are now worried about being “Amazoned.”. The new watch word should be “Be afraid. Be very afraid.” In his session 21st Cloud Expo, Chris Kocher, a co-founder of Grey Heron, will address questions such as: What new areas is Amazon disrupting? How are they doing this? Where are they likely to go? What are th...
SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
Infoblox delivers Actionable Network Intelligence to enterprise, government, and service provider customers around the world. They are the industry leader in DNS, DHCP, and IP address management, the category known as DDI. We empower thousands of organizations to control and secure their networks from the core-enabling them to increase efficiency and visibility, improve customer service, and meet compliance requirements.
Join IBM November 1 at 21st Cloud Expo at the Santa Clara Convention Center in Santa Clara, CA, and learn how IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Cognitive analysis impacts today’s systems with unparalleled ability that were previously available only to manned, back-end operations. Thanks to cloud processing, IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Imagine a robot vacuum that becomes your personal assistant tha...
SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, will lead you through the exciting evolution of the cloud. He'll look at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering ...