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

Posted November 1, 2006 at 8:22 AM by Ben Nadel

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

Nov 1, 2006 at 10:42 AM // reply »
7 Comments

what is the other methods beside GetRequestUrl() ?


Nov 1, 2006 at 11:33 AM // reply »
4 Comments

Ben, Nice job. I see more and more of your postings lately... frankly I just don't know how you find the time ;)

Patrick, It looks like you can get more information on the methods available from: http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html

Although the version of the SDK might be different, it's a good starting point.


Nov 1, 2006 at 2:19 PM // reply »
11,246 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 :(


Jan 10, 2007 at 10:34 AM // reply »
2 Comments

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!


Jan 10, 2007 at 10:35 AM // reply »
2 Comments

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!


Jan 10, 2007 at 10:41 AM // reply »
11,246 Comments

Susan,

No problem! Please let me know if you ever get in a jam. More than happy to help.


May 8, 2008 at 11:46 AM // reply »
1 Comments

Thanks for the code. I've been looking for a way to do this for a while now. Much appreciated!


May 30, 2008 at 9:53 AM // reply »
8 Comments

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. :)


Jun 2, 2008 at 9:30 AM // reply »
11,246 Comments

@Topper,

I have a very active mind. The more I program, the more problems I need to solve :)


dcs
Jun 23, 2008 at 10:50 PM // reply »
25 Comments

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.


Sep 17, 2008 at 5:33 PM // reply »
1 Comments

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!


Mar 25, 2009 at 3:29 PM // reply »
1 Comments

What about url like this?

http://www.mydomain.com/index.cfm#fragmentid

Isn't there a way to just return the entire URL intact rather than trying to piece it back together?


Jul 16, 2009 at 8:21 PM // reply »
1 Comments

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!


Nov 2, 2009 at 11:37 AM // reply »
1 Comments

Thanks Ben, very useful.


Ben
Sep 18, 2012 at 10:24 AM // reply »
3 Comments

Hey all! Just stumbled on this trying to determine ORIGINAL URL request, prior to rewrites, etc. Found this: http://stackoverflow.com/questions/579350/how-do-you-see-the-client-side-url-in-coldfusion

and the excellent reference to GetHttpRequestData() - which contains the headers of the request, including "X-Original-URL" header.

Hope this helps someone!



Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:01 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Avi, Really glad to help! @Jaredwilli, I'm finding a this image hits home with a lot of people :) Hopefully we can all work through the rough patches together! @Prateek, AngularJS has error ... read »
May 25, 2013 at 9:53 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Mrsean2k, I'm glad I could help! I haven't been able to keep up with the ui-router stuff. I keep saying that I'll carve out time, but I just haven't gotten to it :( ... read »
May 25, 2013 at 9:49 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, Thanks for the book recommendations. I am looking them up right now. I can see that Object Thinking is available for the Kindle App - sweet! Also, I just recently heard Martin Fowler on the ... read »
May 25, 2013 at 9:41 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
@Chris, I'm super excited to hear that my posts are helpful. I am also loving AngularJS; but, it definitely has some caveats and some odd behaviors and some things that just don't seem to "wor ... read »
May 25, 2013 at 9:36 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam, @Jason, After reading these comments, I double-checked my latest implementation and I am happy to report that I am using listFirst() and listRest(). ... read »
May 25, 2013 at 9:31 PM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Daxesh, I am not sure I understand the question about the current node. If you already have a reference to the current node, why would you need to query for it? As for parent node, I believe that ... read »
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools