Skip to main content
Ben Nadel at dev.Objective() 2015 (Bloomington, MN) with: Matt Osbun
Ben Nadel at dev.Objective() 2015 (Bloomington, MN) with: Matt Osbun ( @MattOsbun )

GetPageContext() Is Template Specific, Not Request Specific

By on
Tags:

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
5790031

Sub 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

15,674 Comments

@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?

25 Comments

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.

5 Comments

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 :(

15,674 Comments

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.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel