Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HTTP session Vs Request.getSession()

 
vikasids sharma
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
can anybody clarify me the difference between using HTTPSession session.setAttribute("",""), and request.getSession().setAttribute("","");
1. How the scope of these two session object are different?
thanks in advance
Vikas
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are same provided the session has not already timed out.
 
vikasids sharma
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By saying that
"Both are same provided the session has not already timed out."
Do we mean if session times out , we get different types of exception depending upon the classes(HTTPSession and Request) of session object?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Vikas! Both the above are same.
Session is obtained by calling request.getSession().
So session .setAttribute(..) is same as request.getSession().setAttribute(..)
 
vikasids sharma
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Pradeep
Originally posted by Pradeep Bhat:
Sorry Vikas! Both the above are same.
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getSession() returns a session if one exists or creates a new one if it does not. It is the same as calling request.getSession(true). Since retrieving a session goes like this:
HttpSession session = request.getSession();
or:
HttpSession session = request.getSession(true);
Then:
HttpSession session = request.getSession();
session.setAttribute(Object a, Object b);
would have to be the same as:
request.getSession().setAttribute(Object a, Object b);
or:
request.getSession(true).setAttribute(Object a, Object b);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic