JSP: storing Object to PageContext

B

bruce_phipps

JSPs seem to offer more than one way of storing Objects to a session
context for session tracking purposes.

Both these seem to work Ok in my JSP code on Tomcat 5.0.x:

// using pageContext with session scope
pageContext.setAttribute("name", myObject, PageContext.SESSION_SCOPE);

// using the session context of HttpServlet
session.setAttribute("name",myObject);

Whats the difference here? Is there a preferred way to do this?

Thanks
Bruce
 
M

Manish Pandit

JSPs seem to offer more than one way of storing Objects to a session
context for session tracking purposes.

Both these seem to work Ok in my JSP code on Tomcat 5.0.x:

// using pageContext with session scope
pageContext.setAttribute("name", myObject, PageContext.SESSION_SCOPE);

// using the session context of HttpServlet
session.setAttribute("name",myObject);

Whats the difference here? Is there a preferred way to do this?

Thanks
Bruce

Under the hood, they are same. PageContext is a wrapper allowing access
to all scopes (application, session and request), as well as the
implicit objects (request, response). Personally I prefer the
session.setAttribute(name,value) as it is more reader-friendly when
looking at the code.

-cheers,
Manish
 
B

bruce_phipps

Under the hood, they are same. PageContext is a wrapper allowing access
to all scopes (application, session and request), as well as the
implicit objects (request, response). Personally I prefer the
session.setAttribute(name,value) as it is more reader-friendly when
looking at the code.

-cheers,
Manish

Thanks, Manish. As I suspected, they both do exactly the same....

Bruce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top