Wednesday 4 September 2013

session management


How Session management works in the servlets? How Session is maintained by the server, in other words how the server is able to recognise and remember the client across multiple requests. Here you have to remember one thing is that Http is a stateless protocol. So Http in itself does not have the ability to remember the client. remember from where the request is come. So you said a request from your computer to a server ones and Http passes that request to the server.
But you when you send a second request from ur computer http doesn't know that the second request has come from the same computer from where the first request has come so basically HTTP is a stateless protocol. It does not have the ability to store the state of the client or the user.
so maintaining the state something that the server has to do so how does it perform.
For examples when you login your yahoomail, you enter your user name and password and you send a request to the yahoo server, the yahoo server gets you back the inbox and other email messages you have. So basically you click on one of the messages and you read on that message again navigate to various pages in that website and throughout this process server is able to remember you. So the first you enter your username and password and enter in your inbox and navigate across pages at each request the server knows that this is you is asking for details and server gives only your details so the server is able to remember the user.
How does it happen? so lets have a look.
Lets take a example where client A sends a request, say client A is trying to access the yahoo mail and it sends a request to the username of "raj" so now yahoo server takes the request and sees and understands that it is a new request from "raj" so let me store its state in the HttpSession object its basically creates a new HttpSession object (java object) (you can see in the figure ID#99 "raj") and it stores the value for that  HttpSession object with the associates with the user which has sent to the request so it is created an ID for the HttpSession and the id is 99 and it also associated id with the user which actually sent to the request so the user is "raj". So it has associated with the HttpSession object id 99 with the user "raj". Basically creates a HttpSession and sets the attribute "raj"  to this HttpSession  (you can see in the figure after 3 rd step) and HttpSession and then it gives back response to the client and it also gives back the id of the HttpSession created it also called as JSession Id.
So what happend when you sent a request to your yahoo mail inbox, the server has created a new HttpSession object and send back response that is nothing but your inbox page along with that is also send back to ID and that id will help the server to remember you in the future, so what happens now you got your inbox and you want to check your mail.
click on your one of your email messages so again a request is sent to the server
And now when a second request is sent along with the request even the ID #99 . Earlier server has sent back, the same id is sent to the server and now when the server gets second request, then its checks "Do I have a HttpSession object for this request for this user? Oh! and it checks with the user id #99 , ok it matches, with this user and associated with this id#99.
Now the server is able to validate the user and able to remeber the user this request so it knows that this is "raj" whose asking for his inbox details so it gives the message back to the user.
So in this the server of the Web Container manages the sessions across multiple requests



You may like the following posts:

HttpSession
1. cookies.
2. 
URL Rewriting

No comments:

Post a Comment