Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this are same or different pageContext Scope and some of Implicit objects things in JSP Page?

Tags:

java

jsp

This things are same or different if different then please give me reason for that and How?

this things are in scriplet..

1) pageContext.setAttribute("first","value1",pageContext.REQUEST_SCOPE);

and

request.setAttribute("first","value1");

2) pageContext.setAttribute("first","value1",pageContext.SESSION_SCOPE);

and

session.setAttribute("first","value1");

3) pageContext.setAttribute("first","value1",pageContext.APPLICATION_SCOPE);

and

application.setAttribute("first","value1");

like image 448
Vishal Shah Avatar asked Sep 29 '13 10:09

Vishal Shah


People also ask

How many scopes are in PageContext object?

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.

Which JSP implicit object can be used to set data into all scopes?

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.

What are the different scope available in JSP?

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.

What is difference between page and PageContext in JSP?

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.


1 Answers

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");

like image 154
Ramiro Sánchez Avatar answered Sep 30 '22 22:09

Ramiro Sánchez