GetPageContext() Is Template Specific, Not Request Specific
This morning, I was fooling around with some ColdFusion GetPageContext() stuff when I happened upon a little interesting fact: GetPageContext() is template specific. Now, I know very little about Java and what the actual page requests consist of, but I had just always thought that there was one GetPageContext() object per page request. But, as it turns out, there is a different "context" object per ColdFusion template.
To demonstrate this, I am merely getting the HashCode() of the GetPageContext() on two different templates. On each of the templates, I call the HashCode() twice to demonstrate that the multiple calls on the same template have the same outcome and that calls across templates have different outcomes.
Here is the main template:
<cfoutput>
<p>
Main Template:
</p>
<p>
#GetPageContext().HashCode()#<br />
#GetPageContext().HashCode()#<br />
</p>
<cfinclude template="include.cfm" />
</cfoutput>
And, here is the included template:
<cfoutput>
<p>
Sub Template:
</p>
<p>
#GetPageContext().HashCode()#<br />
#GetPageContext().HashCode()#<br />
</p>
</cfoutput>
Running the above main template, I get the following output:
Main Template:
5790031
5790031Sub Template:
5205615
5205615
Notice that the GetPageContext() is returning a different object for each of the two different templates. Very interesting. I am sure some of you smarter fallas out there are gonna look at this and think, "D'uh, what did you think it was gonna do?" Yeah, well I am barely able to wrap my head around the massive page context object :) There is so much to here to dig through.
Want to use code from this post? Check out the license.
Reader Comments
It is indeed page specific. The clue is in the name ;)
To get the underlying request object, do GetPageContext().getRequest()
The page context is a PageContext java object. Look at the javadoc here: http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/jsp/PageContext.html
George.
@George,
Multiple versions of the return Page Context might reference the same request object, but how can they be page specific AND return different HashCodes on different templates of the same request? Maybe I am not understanding what a hash code represents?
Ben,
There's a difference between a PageContext and Request. A Request corresponds to an HTTP Request that CF (really, it's underlying JEE server) has been sent. There is a corresponding Response object that contains headers, the outputted content, etc. The PageContext object, on the other hand, is specific to the given page (CFM template) that is currently executing. This is exactly the same paradigm as JSP uses.
So if a request requires exactly one CFM template, there will be one Request, one Response, and one PageContext that wraps them. If it requires five CFM templates, there willl still be on Request, one Response, but five PageContext objects. This gets a bit muddied when you get into CFC land, as you might expect, but the basic idea holds.
I wasn't particularly clear :s
GetPageContext() returns an object that gives the page the tools it needs to do its job... things like the request, response, scopes, session, etc. Each template will have its own page context as it has its own scopes, attributes, etc, so you get the same object returned twice if you call getPageContext() twice on the same template, and why you'll get different contexts for each template (included or otherwise).
Hope this helps. I nearly came up with a brilliant analogy involving making cups of tea but couldn't make it work for the included template :(
I like tea :)
That sounds good guys, thanks for the feedback. I think that gives me a better place form which to do further exploration.