Reputation: 1890
pageContext.setAttribute("first","value1",pageContext.REQUEST_SCOPE);
and
request.setAttribute("first","value1");
Are these two the same or different? What is the purpose of pageContext
and Page in JSP?
Upvotes: 3
Views: 1807
Reputation: 2417
Both these lines do the same thing, create a new attribute in request scope. If you look at PageContext
class, it has a method initialize, in that it stores a reference to request
object. So when you do pageContext.setAttribute("first","value1",pageContext.REQUEST_SCOPE);
, it creates a new attribute in request scope.
what is the purpose of pageContext and Page in JSP.
pageContext
provides few other useful methods, such as findAttribute
(inhertied method from JspContext) which you can use when you don't know the scope in which attribute is stored.
pageContext
is of type javax.servlet.jsp.PageContext
which is an abstract class. Purpose of this class is to provide single object which exposes utility methods for both page authors and container implementers. As per API DOC,
- Methods Intended for Container Generated Code
Some methods are intended to be used by the code generated by the container, not by code written by JSP page authors, or JSP tag library authors.
The methods supporting lifecycle are initialize() and release()
The following methods enable the management of nested JspWriter streams to implement Tag Extensions: pushBody()
- Methods Intended for JSP authors
The following methods provide convenient access to implicit objects:
getException(), getPage() getRequest(), getResponse(), getSession(), getServletConfig() and getServletContext().
The following methods provide support for forwarding, inclusion and error handling: forward(), include(), and handlePageException().
Upvotes: 1