A session is used to maintain a conversational state in a web application with the client(browser). A session is identified by an unique ID which is called as a 'session id'. This is generated by the web container and it is managed by the container. A session can be created using a HttpSession object. We can observe the object using the HttpServletRequest interface.
HttpSession session = request.getSession(); (request is a HttpServletRequest)
Now a session is created and we can use it to set attributes for a particular user. If we say for example you are at amazon.com and you add a book to your shopping cart. Then you add another book. So in the shopping cart the cost must be the total of both books. It is the session who helped us to store those values as attributes and get the total later. We can do it in this way.
String value1 = ....//get the value of the first book
session.setAttribute("value1" , value1);
// in a separate page we can get the value
String value1 = (String) session.getAttribute("value1");
//then cast it to Integer and do the calculation with the second books value
using this way we can use many attributes in a particular users session. later we can end the session say the user clicks the signout button using the invalidate() method in the session object.
session.invalidate();
then all the attributes we have used for a user may be garbage collected (eligible).
Monday, November 3, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment