This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JspContext and PageContext - Doubt on HFSJ

 
Xiaoxiao Lam
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
On page 518 (Bullet Points) of Book HFSJ, it says:

"The getJspBody()method returns a JspFragment which has two methods: invoke(java.io.Writer) and getJspContext()that returns a JspContext the tag handler can use to get access to the PageContext API (to get access to implicit variables and scoped attributes)."

I don't think this sentence is right. PageContext class extends JspContext class. If getJspContext()returns a JspContext, we can't use it to get access to PageContext's Fields. Or I misunderstood anything. Please help me understand this. Thank you.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "PageContext API" is the general term used to describe all classes relating to JSP contexts - this terminology is for historial reasons, as JspContext was added in JSP 2.0, but PageContext has been around for much longer... The API includes both PageContext and JspContext - and therefore I suppose we could say that JspContext lies at its heart.

It's worth mentioning that in practice, nearly every JspContext instance is in fact a PageContext. The former is designed to be portable across platforms which support JSP but don't necessarily use servlets, while PageContext is designed specifically for servlet environments. Since nearly all JSP servers are J2EE-based, and therefore are built on the servlet model, it is nearly always the case that JspContext == PageContext.

It can actually be very useful to use PageContext directly, as PageContext contains all the constants for the different scopes (e.g. REQUEST_SCOPE), which can be used in JspContext to extract variables from specific scopes (as opposed to a global search using findAttribute()). So it can be useful to have a PageContext occasionally; when this is the case, doing a cast on getJspContext() usually does the job - but you may want to verify (using the instanceof test) that the JspContext is indeed a PageContext first, just to avoid any potential exceptions!

Hope that helps.
 
Xiaoxiao Lam
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Thank you, Charles.
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can put something like this in your tag handler class:

PageContext pc = (PageContext) getJspContext();

I believe as they mentioned in the other answer at this point the containers implementation type an object as JspContext but the instantiated class is PageContext.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic