Dave Carabetta Blog Banner


September 20, 2006

Ten Years Using ColdFusion, And I'm Still Learning!


Sometime over the last couple of weeks, I officially passed the "10 year" mark using ColdFusion. It's not like it's my birthday, so the exact date escapes me, but I remember that it was about a month or so into my sophomore year at George Washington University that I stumbled upon this relatively new scripting language that made building dynamic web sites easy. The rest, as they say, is history. OK, enough nostalgia.

Just over a week ago, Rupesh Kumar, an engineer on the ColdFusion development team in India, posted a great tip for handling J2EE sessions with cookies disabled. Since my company's site requires cookies to be enabled to work (it's part of the service agreement since we're a subscription-based data provider), I haven't had to really worry about appending the session token to URLs. That being said, Rupesh's tip for seamlessly handling J2EE sessions in either situation is a nice little gem, and one I plan to start employing as I move forward. And the best part about it? The solution is a function that is already built into the ColdFusion language!

The URLSessionFormat() function is smart enough to determine whether or not cookies are enabled in the client's browser, and then append the session token (in the proper format) to the URL that is passed as its argument. Here's an example:

<form action="#urlSessionFormat('form_action.cfm')#">

Here's the output with cookies enabled:

<form action="form_action.cfm">

And here's the same exact code snippet with cookies disabled (tested using the awesome Web Developer Extension for Firefox):

<form action="form_action.cfm;jsessionid=d4305073c05055263551?CFID=502&CFTOKEN=519f9941914049aa-CC1F9E84-BC5D-EFFF-EAB28BC4162757E3&jsessionid=d4305073c05055263551">

How cool is that? Most importantly, notice that the jsessionid value is properly appended to the URL using a semi-colon. A lot of developers simply append the jsessionid as if it were just another URL parameter ("&jessionid=xxxxx"), but, as Rupesh points out in his post (and as it came up on a cf-talk thread recently), you must use the ";jessionid=xxxxx" format after the file name for the web server to properly process the request. Simply using the URLSessionFormat() function takes care of that for you.

It's amazing... Ten years in and I'm still learning new tricks all the time!



Comments
Ben Nadel's Gravatar Dave, how awesome is that. I am learning new stuff every day! That's part of why I think ColdFusion is so awesome. It's so easy in th beginning, but in the long run, there is no shortage of new stuff you can learn to do with it.
# Posted By Ben Nadel on 9/20/06 at 1:54 PM
Jeff Howden's Gravatar The only downside to using this function is that you're forced to track the session from the very first request, rather than initiating the tracking at the point where tracking the session becomes important (upon logging in, adding an item to your cart, etc.).

This may not seem like an issue until you consider search engines, which you'd, ideally, like to show paths through your site without session tokens, but who just so happen to not accept cookies.
# Posted By Jeff Howden on 9/20/06 at 2:26 PM
Sami Hoda's Gravatar I would warn you that Urlsessionformat isn't 100% effective. It will add jsession even when cookies are enabled sometimes.
# Posted By Sami Hoda on 9/20/06 at 3:06 PM
Dave Carabetta's Gravatar Jeff, I agree that it might be tough to retrofit your application for this. However, if you're developing in an environment where there's a chance that cookies might be turned off, this is stuff you should be tracking from whenever the user hits the site for the first time anyway, no?

Sami, do you have any examples? I'm just curious as to whether what you're seeing is a true bug with the function or some edge case.
# Posted By Dave Carabetta on 9/20/06 at 3:34 PM
Jeff Howden's Gravatar Dave, no, you rarely have reason to track session stuff from the very first request. Rather, you really only *need* to begin tracking the session when you store something in the session (like adding the first item to a cart, logging in, etc.).

Like in my previous post, the last thing I want to be doing is muddying up nice, clean URLs on my site with session tokens when Google is indexing my site. Using URLSessionFormat(), I'm unable to achieve that, so I simply choose to not use it.
# Posted By Jeff Howden on 9/20/06 at 6:26 PM

© Dave Carabetta, 2005-2009. This blog licensed under the Creative Commons License. Some rights reserved. This is a personal weblog. The opinions expressed here represent my own and not those of my employer. Blog software provided by Raymond Camden.