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 »
10,640 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 »
10,640 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »