This things are same or different if different then please give me reason for that and How?
1) pageContext.setAttribute("first","value1",pageContext.REQUEST_SCOPE);
and
2) pageContext.setAttribute("first","value1",pageContext.SESSION_SCOPE);
and
3) pageContext.setAttribute("first","value1",pageContext.APPLICATION_SCOPE);
and
application.setAttribute("first","value1");
The PageContext class defines several fields, including PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, and APPLICATION_SCOPE, which identify the four scopes. It also supports more than 40 methods, about half of which are inherited from the javax.
In JSP, pageContext is an implicit object of type PageContext class. The pageContext object can be used to set,get or remove attribute from one of the following scopes: page.
Every object created in a JSP page will have a scope. Object scope in JSP is segregated into four parts and they are page, request, session and application. 'page' scope means, the JSP object can be accessed only from within the same page where it was created.
PageContext extends JspContext to contribute helpful context details while JSP technology is applied in a Servlet environment. A PageContext is an instance that gives access to all the namespaces related to a JSP page, gives access to some page attributes and a layer over the application details.
In JSP pages you have up to "4 places" where you can put objects to retrieve them later.
1) Page scope
Whatever you put into your page scope is available only there. Any other page in the same request included via or forwarded will not see the object since they define their own page scope which does not contain the page scope of the calling page.
This is the default scope, so calling pageContext.setAttribute("a", "b", PageContext.PAGE_SCOPE);
is the same as calling pageContext.setAttribute("a", "b");
2) Request scope
What you put on your request scope is available across all the pages of the request serving this JSP page. So other pages included or forwarded (not HTTP redirect) will share this context and can access the attributes declared in the calling page context.
Calling pageContext.setAttribute("a", "b", PageContext.REQUEST_SCOPE);
is the same as calling request.setAttribute("a", "b");
3) Session scope
What you put on your session scope is available across all requests on the same user session.
Calling pageContext.setAttribute("a", "b", PageContext.SESSION_SCOPE);
is the same as calling session.setAttribute("a", "b");
4) Application scope
What you put on your application scope is available across all requests on your application (i.e. is shared by all users). This implies a lifetime that is basically as long as the application is running. So you generally don't want to use this one.
Calling pageContext.setAttribute("a", "b", PageContext.APPLICATION_SCOPE);
is equal to calling application.setAttribute("a", "b");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With