Powered by Blogger.

Saturday 14 March 2015

Session Tracking

No comments :
A session is a conversation between the server and a client. A conversation consists series of continuous request and response. HTTP is a stateless protocol. Each request is independent of the previous one. However, in some application, it is necessary to save state information so that information can be collected from several interactions between a browser and a sever. Sessions provide such a mechism. A session can be created via the getSession() method of HttpServletRequest. An HttpSession object is returned. This object can store a set of bindings that associate names with objects. The setAttribute(), getAttributeName(), and removeAttribute() methods of HttpSession manage these bindings. Session State is shared by all servlets that are associsted with a 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 getAttribute() method is called to obtain the object that is bound to the name "date". That object is a Date object that encapsulations the date and time when this page was last accessed. A Date object encapsulating the current date and time is then created. The setAttribute() method is called to bind the name "date" to this object.
Example : 1. Session Tracking code: index.html



2. Session Tracking code: web.xml



3. Session Tracking code: UserVisitServlet.java



when you first request this servlet this servlet, the browser display one line with the current date and time information. On subsequent invocations, two lines are displayed. The first line show the date and time when the servle was last accessed. the second line show the current date and time.

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.