Forums Register Login

pageContext why?

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
i meant ..
i am sure.. i am missing out something
+Pie Number of slices to send: Send
 


i am missing out something



Geetu, yes you are missing lot of letters in your words as pointed by Bear Bibeault.
+Pie Number of slices to send: Send
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...???
+Pie Number of slices to send: Send
 


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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 


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.
+Pie Number of slices to send: Send
 


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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 


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.
+Pie Number of slices to send: Send
 

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..
+Pie Number of slices to send: Send
In over 7 years of writing JSPs, I have never found a use for the implicit page object.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
 

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 ..
+Pie Number of slices to send: Send
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.
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 13005 times.
Similar Threads
PageContext
JSP implicit objects in Simple Tags
How to Count Num. of Objects
page scope - pageContext related?
pageContext
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:53:29.