Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Maintain Session  RSS feed

 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If HTTP is staleless protocol then how does it maintain Session through HttpSession

Thanks,
aakash
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The server uses a session cookie (stored in the user's browser), or, sometimes a url parameter to match the user up to a session object (stored in memory on the server).
 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben,
With cookie and Url paramter we can maintain session indirectly.
But my query is as Http is a staless protocol then how does we can maintain a sesion by setting values in HttpSession, then how does in this HTTP protcol handle it.

Thanks,
aakash
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, HTTP is stateless. That means the server has to check for a sessionId in every request that comes in (either as a cookie header or as a url parameter).
If it finds one, it checks to see if there is an object, stored in session, with a matching id. If not, then it creates a new object and sends the ID back to the browser in the response.

The HTTP protocol doesn't handle it. The servlet container mimicks a stateful session with this technique.

If you're asking how to save objects in session, read up on the HttpSession's
getAttribute and setAttribute methods.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!