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:
Here's the output with cookies enabled:
And here's the same exact code snippet with cookies disabled (tested using the awesome Web Developer Extension for Firefox):
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!
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.
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.
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.