Session Management
Session Management
connection to the Web server and the server automatically does not keep any record of previous client request. Still there are following three ways to maintain session between web client and web server:
(1) Cookies:
A webserver can assign a unique session ID as a cookie to each web client and for subsequent requests from the client they can be recognized using the recieved cookie. This may not be an effective way because many time browser does nots upport a cookie, so I would not recommend to use this procedure to maintain the sessions.
S.N. 1
Method & Description public Object getAttribute(String name) This method returns the object bound with the specified name in this session, or null if no object is bound under the name. public Enumeration getAttributeNames() This method returns an Enumeration of String objects containing the names of all the objects bound to this session. public long getCreationTime() This method returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT. public String getId() This method returns a string containing the unique identifier assigned to this session. public long getLastAccessedTime() This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT. public int getMaxInactiveInterval() This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses. public void invalidate() This method invalidates this session and unbinds any objects bound to it. public boolean isNew( This method returns true if the client does not yet know about the session or if the client chooses not to join the session. public void removeAttribute(String name) This method removes the object bound with the specified name from this session. public void setAttribute(String name, Object value) This method binds an object to this session, using the name specified. public void setMaxInactiveInterval(int interval) This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
4 5
7 8
9 10 11
<%@ page import="java.io.*,java.util.*" %> <% // Get session creation time. Date createTime = new Date(session.getCreationTime()); // Get last access time of this web page. Date lastAccessTime = new Date(session.getLastAccessedTime()); String title = "Welcome Back to my website"; Integer visitCount = new Integer(0); String visitCountKey = new String("visitCount"); String userIDKey = new String("userID"); String userID = new String("ABCD"); // Check if this is new comer on your web page. if (session.isNew()){ title = "Welcome to my website"; session.setAttribute(userIDKey, userID); session.setAttribute(visitCountKey, visitCount);
} visitCount = (Integer)session.getAttribute(visitCountKey; visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); session.setAttribute(visitCountKey, visitCount); %> <html> <head> <title>Session Tracking</title> </head> <body> <center> <h1>Session Tracking</h1> </center> <table border="1" align="center"> <tr bgcolor="#949494"> <th>Session info</th> <th>Value</th> </tr> <tr> <td>id</td> <td><% out.print( session.getId()); %></td> </tr> <tr> <td>Creation Time</td> <td><% out.print(createTime); %></td> </tr> <tr> <td>Time of Last Access</td> <td><% out.print(lastAccessTime); %></td> </tr> <tr> <td>User ID</td> <td><% out.print(userID); %></td> </tr> <tr> <td>Number of visits</td> <td><% out.print(visitCount); %></td> </tr> </table> </body> </html>
Now put above code in main.jsp and try to access http://localhost:8080/main.jsp. It would display the following result when you would run for the first time:
Welcome to my website
Session Infomation
Session info id Creation Time Time of Last Access User ID Number of visits value 0AE3EC93FF44E3C525B4351B77ABB2D5 Tue Jun 08 17:26:40 GMT+04:00 2010 Tue Jun 08 17:26:40 GMT+04:00 2010 ABCD 0
Now try to run the same JSP for second time, it would display following result.
Session Infomation
info type id Creation Time Time of Last Access User ID Number of visits value 0AE3EC93FF44E3C525B4351B77ABB2D5 Tue Jun 08 17:26:40 GMT+04:00 2010 Tue Jun 08 17:26:40 GMT+04:00 2010 ABCD 1