This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes pageContext why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "pageContext why?" Watch "pageContext why?" New topic
Author

pageContext why?

geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
I am not very clear with the concept of pageContext. Y do you need this object.. the attributes can be in the request as well right??
coz mostly.. the requests would not invlove multiple pages..

Also, can some 1 explain that the included jsp page will share the same pagecontext or wil have a different

m sure.. m siing out sumthing.. coz else if pagecntxt wasnt so imp.. el would not be widely using it...
[ March 19, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

m sure.. m siing out sumthing.. coz else if pagecntxt wasnt so imp..


What is this gibberish?
[ March 19, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
i meant ..
i am sure.. i am missing out something
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

i am missing out something


Geetu, yes you are missing lot of letters in your words as pointed by Bear Bibeault.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
in short..
i want to know.. the significance of pagecontext ....
why not request object instead of pagecontext.. coz request is normally not across many pages...???
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

coz request is normally not across many pages...???


Thats NOT quite correct.

Request attibutes can be accessed across multiples pages as long as request is part of the same request chain. Whereas attributes in pageContext are accessible ONLY in that page.
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
Request attibutes can be accessed across multiples pages


yes,but normally it is through <jsp:include>.. which according to my understanding.. the included page shares the same page context...
and forward would invoke another request...
so the request scope should suffice..
pagecontext in ths case is nt limiting the scope of the variable
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


Whereas attributes in pageContext are accessible ONLY in that page.


page and pageContext are entirely different and the above is not correct.

'page' is the 'this' object or the current object. It refers to the instance of the Servlet that was created from jsp translation and which is currently being executed to display the response. 'page' is also one of 4 the scopes available in a j2ee web application. It is infact the most restricted and is confined to the jsp page in which the scoped attribute is defined. Thus if you define an attribute and bind it to a page scope, this attribute will not be visible outside the page in which it is defined. 'page' instances are of type javax.servlet.jsp.JspPage

pageContext is an implicit object (just like session, request etc) available to all jsps and is of type javax.servlet.jsp.PageContext. It provides somwhat similar functionality to a jsp as the ServletContext provides to a Servlet. The java doc lists these functionalities

The PageContext provides a number of facilities to the page/component author and page implementor, including:

1. a single API to manage the various scoped namespaces
2. a number of convenience API's to access various public objects
3. a mechanism to obtain the JspWriter for output
4. a mechanism to manage session usage by the page
5. a mechanism to expose page directive attributes to the scripting environment
6. mechanisms to forward or include the current request to other active components in the application
7. a mechanism to handle errorpage exception processing

Most importantly pageContext is not a scope by itself - variables/atttributes cannot be bound to pageContext. The pageContext object has apis that help the developer bind the attributes to any one of the scopes.

Thus,

pageContext.getAttribute(String name) returns the object associated with the name in the page scope or null if not found.

pageContext.getAttribute(String name,int scope) returns the object associated with the name in the specified scope or null if not found.

pageContext.findAttribute(String name) searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

The same goes for setting and removing attributes.

You can read the entire javadoc for pageContext here

cheers,
ram.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

page and pageContext are entirely different and the above is not correct.


Why not correct?

pageContext.setAttibute("abc","xyz");

Here the object "xyz" is stored with the name "abc" in page scope. I never said page and pageContext are same.

I was talking about the setAttribute method that doesn't take a scope parameter.


Whereas attributes in pageContext are accessible ONLY in that page.


Probably the confusion is with my wording. Let me rephrase my words.

Attributes set in page scope using pageContext implicit object are accessible only within the page.

NOTE: page scoped attributes can ONLY be set using pageContext implicit object.
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
ok..
i am now convinced with the pagecontext concept..
want to know more...

if i use a custom tag in the jsp.. will the same pagecontext be shared between the page and the tag???

also.. i read that 'page' implicit object is not normally used by the jsp authors.. so then who uses it and for what purpose
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

i read that 'page' implicit object is not normally used by the jsp authors.. so then who uses it and for what purpose


Every JSP Page is traslated into java code and compiled into servlet class. "page" implicit object is just like "this" keyword that's used to refer to the instance of the generated servlet class from your JSP page.
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
like "this" keyword that's used to refer to the instance of the generated servlet class from your JSP page.


i understood this..
but about practicality, where do i use this..
how do i use the 'page' object

if it is as good as the generated servlet.. what purpose it serves..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

In over 7 years of writing JSPs, I have never found a use for the implicit page object.
Thibault Dangr�aux
Greenhorn

Joined: Dec 12, 2005
Posts: 19
Originally posted by geetu lalchandani:
if i use a custom tag in the jsp.. will the same pagecontext be shared between the page and the tag???


Yes. That's used by the i18n tags for instance. The fmt:setFormat tag will set the locale inside the pageContext (by default) and all the other i18n tags will read it from there.
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
Originally posted by geetu lalchandani:


i understood this..
but about practicality, where do i use this..
how do i use the 'page' object

if it is as good as the generated servlet.. what purpose it serves..



If you are aware, it is possible to write JSP's class level variables and methods using
<%!
%>.
The code written within above are not generated within the _jspService() method, instead they are generated under JSP's class code.

If you have defined any variable within this scope which clashes with the variable name defined in _jspService(), then you might need to use this.

There is also possibility to write custom Base Class for all the JSPs class which are generated. In that case too, we might need to use this.

Regarding page object, it is just matter of convenience for calling Servlet methods. But I think it is the type HttpJspPage or JspPage.

So you cannot call, page.someCustomMethod(), which you have written with

<%!
%>
block.


Manesh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Originally posted by Dilip Kumar Jain:

If you are aware, it is possible to write JSP's class level variables and methods ...


Though highly discouraged at this juncture since better patterns of use have emerged.
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
Originally posted by Bear Bibeault:


Though highly discouraged at this juncture since better patterns of use have emerged.


I too agree with you. But I had no another scenario to better explain that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Understood. I just didn't want any newcomers to get the impression that that's still an accepted practice.
[ March 21, 2006: Message edited by: Bear Bibeault ]
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
If you are aware, it is possible to write JSP's class level variables and methods using
<%!
%>.
The code written within above are not generated within the _jspService() method, instead they are generated under JSP's class code.


i am sorry guys, but i ddnt understand.. why ths came into picture? this has no reference to the 'page' object.

i know that can be done and uses 'this' reference, bt i think i am missing something you want to expalin ..
Monikaa Agassi
Greenhorn

Joined: Jan 30, 2006
Posts: 5
Hi,
I am unable to access pageContext in JSP errorPage. I am defining the errorPage in which i need pageContext. I am using JSP 1.2 on Jboss 2.4.11 with Tomcat 4.0.6.

I can access request , but not pageContext.

Is there any reason why i am not getting pageContext (global object) in error.jsp ?

Thanks in advance.
 
GeeCON Prague 2014
 
subject: pageContext why?