I am new to Java EE. I have a site which requires a user to log in, and after the user logs in I would like the user to see his/her own item (e.g: shopping cart).
So that means I have to use a session to accomplish that. But how do I deal with multiple sessions?
For example multiple users login to the system as well as to the same servlet? However, can I keep multiple sessions in one servlet? Do I keep a record of each of them? How does it work?
Can someone give an example please ?
In servlet you have access to
HttpServletRequestwhich provides you with agetSession()method. This methods returns session object (essentialy a key-value map), different for each user.If you put something into that map (like shopping cart), you can retrieve it later – but only when the same user accesses the application again (not necessarily the same servlet).
Sessions are maintained in the servlet container and looked up by session id (typically a cookie). You don’t have to implement anything yourself.