CGI Variables and Their Respective ColdFusion/Java Servlet Alternative Methods

Are you a ColdFusion developer? Have you ever wanted to get away from using CGI variables such as CGI.SCRIPT_NAME but didn't quite know what else to use? Then you might find the following list of CGI variables and their respective ColdFusion/Java Servlet Alternative methods to be helpful because I couldn't find much (if any) documentation on this:

CGI Variable ColdFusion / Java Servlet Alternative Method
CGI.AUTH_TYPE getPageContext().getRequest().getAuthType()
CGI.CONTENT_LENGTH getPageContext().getRequest().getContentLength()
CGI.CONTEXT_PATH getPageContext().getRequest().getContextPath()
CGI.CONTENT_TYPE getPageContext().getRequest().getContentType()
CGI.PATH_INFO getPageContext().getRequest().getPathInfo()
CGI.PATH_TRANSLATED getPageContext().getRequest().getPathTranslated()
CGI.QUERY_STRING getPageContext().getRequest().getQueryString()
CGI.REMOTE_ADDR getPageContext().getRequest().getRemoteAddr()
CGI.REMOTE_HOST getPageContext().getRequest().getRemoteHost()
CGI.REMOTE_USER getPageContext().getRequest().getRemoteUser()
CGI.REQUEST_METHOD getPageContext().getRequest().getMethod()
CGI.SCRIPT_NAME getPageContext().getRequest().getServletPath()
CGI.SERVER_NAME getPageContext().getRequest().getServerName()
CGI.SERVER_PORT getPageContext().getRequest().getServerPort()
CGI.SERVER_PORT_SECURE getPageContext().getRequest().isSecure()
CGI.SERVER_PROTOCOL getPageContext().getRequest().getProtocol()
CGI.SERVER_SOFTWARE getPageContext().getRequest().getHeader("Server-Software")
CGI.HTTP_ACCEPT getPageContext().getRequest().getHeader("Accept")
CGI.HTTP_ACCEPT_CHARSET getPageContext().getRequest().getHeader("Accept-Charset")
CGI.HTTP_ACCEPT_ENCODING getPageContext().getRequest().getHeader("Accept-Encoding")
CGI.HTTP_ACCEPT_LANGUAGE getPageContext().getRequest().getHeader("Accept-Language")
CGI.HTTP_CONNECTION getPageContext().getRequest().getHeader("Connection")
CGI.HTTP_COOKIE getPageContext().getRequest().getHeader("Cookie")
CGI.HTTP_HOST getPageContext().getRequest().getHeader("Host")
CGI.HTTP_REFERER getPageContext().getRequest().getHeader("Referer")
CGI.HTTP_USER_AGENT getPageContext().getRequest().getHeader("User-Agent")
Other Useful Java Servlets  
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters. getPageContext().getRequest().getRequestURL()
The URL string corresponding to the current client request. The string returned does not include the server name or query arguments. getPageContext().getRequest().getRequestURI()
Returns the name of the scheme used to make this request (http, https, or ftp) getPageContext().getRequest().getScheme()

Here's some code you can use to copy and paste to your .cfm file for testing:

    <table border="1">
            <th>CGI Variable</th>
            <th>ColdFusion / Java Servlet Alternative Method</th>
            <td>getPageContext().getRequest().getPathTranslated() </td>
            <td colspan="4"> </td>
            <td><em><strong>Other Useful Java Servlets</strong></em></td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td>Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it <em>does not include</em> query string parameters. </td>
            <td> </td>
            <td>The URL string corresponding to the current client request. The string returned does not include the server name or query arguments.</td>
            <td> </td>
            <td>Returns the name of the scheme used to make this request (http, https, or ftp)</td>
            <td> </td>

Obviously, you should test these on your server before using them in production. Some of the methods may or may not be available. For example, I get empty strings using getPathInfo() and getPathTranslated(). You might want to try something like the following to see what methods are available on your server:

    <cfset availMethods = GetPageContext().getRequest() />
    <cfdump var="#availMethods#" />

Here's some more interesting information. Let's say you've defined some variables in a 'globalvariables.cfm' file and included them in your Application.cfc template, such as:

    <cfset REQUEST.RootDirectory = expandpath('/') />
    <cfset REQUEST.CurrentDirectory = GetDirectoryFromPath(GetTemplatePath()) />
    <cfset REQUEST.CurrentPage = GetFileFromPath(GetBaseTemplatePath()) />

If you run the following code, you should see what you also expected to see by simply using <cfoutput>#REQUEST.RootDirectory#</cfoutput> or <cfoutput>#REQUEST.CurrentDirectory#</cfoutput>.

    #getPageContext().getRequest().RootDirectory#<br />
    #getPageContext().getRequest().CurrentDirectory#<br />

Just for fun, try this too:

    <cfset dirlist = getPageContext().getRequest().CurrentDirectory />
        <cfloop list="#dirlist#" delimiters="\" index="i">

For more information on the Java HttpServletRequest:

For more informaiton on HTTP Header Field Definitions, see:

If you catch a typo or have some other insight, please feel free to add to this.

Interesting that there's helper functions for some of the more common CGI variables. It would probably be more useful if you were needing several of these, or doing it dynamically, to just use the getHeader function for everything.

Also, CF returns true whenever you use IsDefined for any CGI variable, regardless of if it actually exists or not. These Java functions return nulls for non-existent CGI variables, which is probably slightly more useful. I think you could then do:
x = getPageContext().getRequest().getHeader("foobar")
y = IsDefined("x")
# Posted By duncan | 8/26/08 5:24 PM
Duncan, you bring up some interesting thoughts. I tested the getHeader() function for a few other CGI variables and now I'm trying to figure out where getHeader() is actually pulling its information from. Does getHeader() pull the info directly from CGI?

The reason I'm wondering is because as I mentioned in my article, when I use getPathInfo(), I get an empty string. However when I use CGI.PATH_INFO _or_ getHeader("path-info"), I get the value I was looking for.
# Posted By Stephen Withington | 8/27/08 7:24 AM

I think your post is neat. It's always fun to read about different ways to use java in CF. I have to ask though why? Just to do it or would their be a situation or two where it would be better to use the java methods vs the CGI methods. Using CGI seems to be a lot less typing.


# Posted By Randy | 9/4/08 11:58 AM

Great question. One situation in particular where it might make more sense to use java vs. CGI would be if (for whatever reason, and no, I haven't personally encountered this) a web server wouldn't return CGI vars. I've seen a few posts by Dan G. Switzer, II ( where he warns against using (or relying on) CGI. For example, see his comments (and other developers) in a post by Ben Nadel at I'm sure there are several other examples as well.

Other than that, it's just fun to learn about the ins-and-outs of ColdFusion. I feel like I'm "looking under the hood" so-to-speak (which might be a bad analogy, but oh well). It always amazes me at how many different ways there are to accomplish something in ColdFusion. It seems like there's a method to fit most any programmer's preferred style.
# Posted By Stephen Withington | 9/4/08 1:31 PM
This was a pretty fun post. Well researched and nicely put together.

I also like to learn more about the Java underpinnings so please share other bits of info as you find it.

# Posted By Dan Wilson | 9/4/08 8:31 PM

Thanks for the comments, and ditto.
# Posted By Stephen Withington | 9/5/08 7:43 AM
One other standard method is GetHTTPRequestData(); returns a structure, and member .headers is a struct of all HTTP headers.

Another HTTP header that doesn't show up in CGI scope, is if you have mod_rewrite or an ISAPI rewrite that sends the original path. For example, Ionics ISAPI rewrite has a rule option U, which sends the original path in header "X-Rewrite-Url"
# Posted By David | 9/9/08 6:05 PM

Thanks for sharing the information. I'll have to play around with that.
# Posted By Stephen Withington | 9/12/08 7:02 AM
Thanks for this article. It provided me with enough information to determine when a web requests passed a bogus "blank" CGI.Content-Type versus not passing on at all. I believe that CGI.Content_Type should always have content when performing a POST and be [Empty String] when performing a GET.

GET = isNull(getPageContext().getRequest().getContentType())
POST = LEN(TRIM(CGI.Content_Type))

[Log Request for later review]
[cfheader statuscode="204" statustext="No Content"][CFABORT]

I had to resort to testing this because the CF9 server was throwing a "String index out of range: -1" error when using GetHttpRequestData() to test headers for an ajax request. PCI compliance testing was causing this and the only difference I could identify between the requests was CGI.Content_Type = '' versus CGI.Content_Type = [Empty String].
# Posted By James Moberg | 2/26/13 8:33 PM
The watch's case is made of stainless steel18k rose gold with a bezel in rose gold and the rest in stainless steel. They are all polished and polished to give a seductive and full look. The lugs are slightly curved, making the watch more conformable to the wearer's wrist and ergonomically set.
# Posted By uk replica watches | 7/23/18 2:06 AM
mobile hairdresser for seniors near me hair extensions alopecia how to treat dry hair extensions type 3 hair armpit length haircuts 2018 balmain hair extensions colours babyliss elegance hair dryer argos coconut oil to remove tape in hair extensions
# Posted By SV | 11/4/18 8:50 PM
Since you've getting done with composing articles regarding this matter, would you be able to please enlighten us concerning alter site now? I have been getting some information about this wesbsite for a long while now.
# Posted By AP pharma-d web counselling | 4/15/20 2:15 AM
Karnataka HRMS Login To Get Employee Salary Bill Transfer Pay Roll Details @ at etc: Karnataka state government opened a Karnataka HRMS portal on employee salary bills services transfer pay roll suspension promotion and all other details regarding to the job of more than five lakh employees. And made a record of only state in the country to have implemented total HRMS.
# Posted By AP pharma-d web counselling | 4/19/20 11:34 AM

© 2021, Stephen J. Withington, Jr.  |  BlogCFC was created by Raymond Camden – Version 5.9.004

Creative Commons License  |  This work is licensed under a Creative Commons Attribution 3.0 Unported License.  |  Hosted by