• Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpSession and session state

 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I want to confirm with the servlet experts out there that what I want to do is valid or if there is a better way, I would like to know that. My understanding of servlets is that they are sessionless. I have an object that I want to initialize in the doGet() method, but I want each user to have their own instance of this object. However, I do not want this object to get initialized every time the <B>SAME</B> user makes a request. So, if I use getSession() method to get an HttpSession and validate that this user still has a session and only initialize the object if the user does <B>not</B> have a current session. Would this be the correct (standard) way to handle this? Thanks for your help!

Barry
 
inam paki
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,
The mechanism/protocol on which HttpServlet operate is Sessionless/stateless not the Servlet in General.
You can create such Servlets who own/manage their Session by them but the difference is that their protocol will be different may be ftp instead of http.
And the second thing about your query is that you can initialize your instance after checking each HttpSession object by the method isNew(); this method returns true if the Session created right now and false otherwise.
for example:
HttpSession session = req.getSession();
if(session.isNew())
//here initialize your object.
this will work.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry Andrews wrote: So, if I use getSession() method to get an HttpSession and validate that this user still has a session and only initialize the object if the user does <B>not</B> have a current session. Would this be the correct (standard) way to handle this?
Almost.
The way most people do this is something like the following:
In each servlet, at the start of doGet, doPost, service etc:
  • get the session. Use the default call which creates a session if it doesn't already exist.
  • attempt to get your data item from the session
  • If your item is not there (getting it returns null) create and initialize one, and store it in the session.
  • If the item is there (getting it returns non-null) use it!


  • Remember that a "session" is effectively just a HashMap of names and values kept on the server, associated with a "session id" which is sent to and from the browser.
     
    Barry Andrews
    Ranch Hand
    Posts: 529
    C++ Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Inam for the comments!
    Frank, I actually thought about this too, but wasn't sure. Attempting to get the object from the HttpSession seems like a good solution. This is more along the lines of what I was looking for; a kind of industry standard way of keeping state. Thanks both of you for the input!

    Barry
     
    Anthony Villanueva
    Ranch Hand
    Posts: 1055
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just to add: servers may persist session data so in this case you must make the data serializable.
     
    Barry Andrews
    Ranch Hand
    Posts: 529
    C++ Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!