GetPageContext() Is Template Specific, Not Request Specific

Posted August 22, 2007 at 8:25 AM by Ben Nadel

Tags: ColdFusion

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.


You Might Also Be Interested In:



Reader Comments

Aug 22, 2007 at 10:53 AM // reply »
5 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.


Aug 22, 2007 at 10:56 AM // reply »
11,246 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?


Aug 22, 2007 at 11:48 AM // reply »
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.


Aug 22, 2007 at 12:07 PM // reply »
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 :(


Aug 22, 2007 at 12:33 PM // reply »
11,246 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.


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 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools