HTIP is a stateless protocol, which means that each request is independent of the -previous one, However, in some applications, it is necessary to save state information so that information can be collected from several.interactions between a browser and 11 server. Sessions provide such a mechanism. A session can be created via the getSession( ) rx method of HttpServletRequest. An HttpScssion object is returned. This object can store a set of bindings that associate names with objects. The putValue(), getValue(), getValueNames(), and removeValue() methods of HttpSession manage these bindings. It is-important to note that session state is shared among all the servlets that are associated with a’ particular client.The following servlet illustrates how to use session state. The getSession() method gets the’ current session. A new session is created if one does not already exist. The getValue() method is called to obtain the object that is bound to the name “date”. That object is a Date, object that encapsulates the date and time when this page was last accessed. (Of course, there is no such binding when the page is first accessed.) A Date object encapsulating the current date and time is then created. The putValue() method is called to bind the name “date” to this object.
When you first request this servIce, the browser displays one line with the current date a .d time information. On subsequent invocations, two lines are displayed. The first line shows the date and time when the servlet was last accessed. The second line shows the current date and time.