Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with:

Getting The Requested URL From The Page Request Object (Servlet) Without Using CGI

By Ben Nadel on
Tags: ColdFusion

Traditionally, I figure out which page URL has been requested by recreating it from various CGI variables:

  • <!--- Get requested URL. --->
  • <cfset strUrl = (
  • IIF(
  • (CGI.https EQ "On"),
  • DE( "https://" ),
  • DE( "http://" )
  • ) &
  • CGI.http_host &
  • CGI.script_name & "?" &
  • CGI.query_string
  • ) />

As you can see, it takes into account CGI.https, CGI.http_host, CGI.script_name, and CGI.query_string. This works totally fine for me. But, I have been told that relying on CGI variables is not a good way to go because every server handles CGI data differently. In another blog post, Dan G. Switzer, II made this comment:

Also, custom header information can be applied by custom filters run within the web server--which is why the CGI scope was designed to return an empty string if the key isn't explicitly defined. One reason I don't rely on the cgi.script_name is that not all web servers return it. I really try to avoid using the CGI scope when at all possible.

Frankly, the idea of not using the CGI scope was a bit scary. How could I figure out the URL? Turns out, you can get it directly from the Request servlet object that is available through the ColdFusion page context:

  • <!--- Get request from ColdFusion page contenxt. --->
  • <cfset objRequest = GetPageContext().GetRequest() />
  •  
  • <!--- Get requested URL from request object. --->
  • <cfset strUrl = objRequest.GetRequestUrl().Append(
  • "?" & objRequest.GetQueryString()
  • ).ToString()
  • />

The GetRequestUrl() returns a string buffer which is why the we call ".ToString()" on the GetRequestUrl() return value; this converts the string buffer to a simple string. Before we convert the url buffer to a string, though, we append the query string to it. As you can see, the query string is also available through the request object. That's all there is to it.

Now getting the requested URL is CGI-agnostic. But, how does the request servlet object know what the URL and query string values are? Frankly, that's not my concern. That's the whole beauty behind black-boxing data retrieval - I don't need to know the implementation of it, I just need to know that it returns the correct values.



Reader Comments

Joshua,

Thanks for posting that link. I was hoping around all over the place looking at different online documentation. The one ColdFusion using is something ColdFusion-specific ( I think ), but it most likely extends this class.

As far as time... I WISH i had more. I get about 5.5 hours of sleep a night, which helps but I could certainly use a few more hours in the day :(

Reply to this Comment

Thank you!!!!!!
most of the time when I search something on the internet it takes time to figure out what I need....with this all I had to do is copy and paste....sooooooo simple.....Thanks!

Reply to this Comment

Thank you!!!!!!
most of the time when I search something on the internet it takes time to figure out what I need....with this all I had to do is copy and paste....sooooooo simple.....Thanks!

Reply to this Comment

Good man Ben.

Other methods using CGI don't work well.

Aside: How do you keep up the pace - everytime I good something ColdFusion related you have it covered. :)

Reply to this Comment

I tried using this for a CF custom 404 page and instead of returning the URL that occasioned the 404 error it returned the URL of the 404 page itself (even though that is not what is displayed in the browser)! Currently I am using CGI.query_string in order to return the errant URL but this solution is definitely not portable.

Reply to this Comment

Thank you so much for this. I was spending a long time beating my head against a wall trying to figure out how to simply grab the entire URL with parameters!

Reply to this Comment

Thank you for this code - it's so much harder to find this stuff for ColdFusion than it is for Apache!

I'm trying to force www., and here's what my final code is:
<code>
<cfif (CGI.SERVER_NAME EQ "example.net")>
<cfset strUrl = CGI.script_name & "?" & CGI.query_string />
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="http://www.example.net#strUrl#">
</cfif>
</code>

Thanks again!

Reply to this Comment

I?m Not That Much Of A Internet Reader To Be Honest But Your Sites Really Nice, Keep It Up! I'll Go Ahead And Bookmark Your Site To Come Back Later. Cheers

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.