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 ]
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...???
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.
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
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 javadoc 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.
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.
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.
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
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 ]
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 ..
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.
Post by:autobot
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards