[ Team LiB ] Previous Section Next Section

Recipe 11.5 Tracking Session Activity in Servlets

Problem

You want to use a servlet to track the creation time and last-accessed time for a session.

Solution

Use the HttpServletRequest object's getSession( ) method to get a reference to the HttpSession object. Then call the HttpSession.getCreationTime( ) and HttpSession.getLastAccessedTime( ) methods on that object.

Discussion

This recipe describes how to use the HttpSession API to find out the creation time and the last-accessed time for a session. How would a web application use this information? For one, you might want to monitor the pattern of request activity in a web application by comparing the session creation time, the last-accessed time, and the current time. For example, the difference between the creation time and the current time (measured in seconds) would indicate how long the web application had been tracking a particular user's session.

The method HttpSession.getLastAccessedTime( ) returns the time (as a long datatype) of the last time the user made a request associated with a particular session.

A servlet that calls getLastAccessedTime( ) represents the most current request associated with the session. In other words, the time at which the user requests the servlet that calls getLastAccessedTime( ) becomes the last accessed time.


Example 11-7 displays the current time, as well as the session's creation and last- accessed times.

The HttpServletRequest.getSession( ) method associates a new session with the request if one does not already exist. The HttpServletRequest.getSession(false) method returns null if a session is not associated with the request and it will not create a new HttpSession for the user. See Recipe 11.4.


Example 11-7. Calling HttpSession methods in a servlet
package com.jspservletcookbook;           

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.Date;
import java.text.DateFormat;
import java.util.Enumeration;

public class SessionDisplay extends HttpServlet {

 public void doGet(HttpServletRequest request, 
   HttpServletResponse response)
     throws ServletException, java.io.IOException {
        
     response.setContentType("text/html");
     java.io.PrintWriter out = response.getWriter( );

     HttpSession session = request.getSession( );

     Date creationTime = new Date(session.getCreationTime( ));

     Date lastAccessed = new Date(session.getLastAccessedTime( ));

     Date now = new Date( );

     DateFormat formatter = 
       DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
       DateFormat.MEDIUM);
        
     out.println("<html>");
     out.println("<head>");

     out.println(
       "<title>Displaying the Session Creation and "+
       "Last-Accessed Time</title>");

     out.println("</head>");
     out.println("<body>");
     out.println("<h2>Session Creation and Last-Accessed Time</h2>");
     out.println(
       "The time and date now is: " + formatter.format(now) +
       "<br><br>");

     out.println("The session creation time: "+
       "HttpSession.getCreationTime( ): " +
       formatter.format(creationTime) + "<br><br>");

     out.println("The last time the session was accessed:  " +
       HttpSession.getLastAccessedTime( ): " + 
       formatter.format(lastAccessed) );

     out.println("</body>");
     out.println("</html>");
 }//doGet

 public void doPost(HttpServletRequest request, 
   HttpServletResponse response)
     throws ServletException, java.io.IOException {

      doGet(request,response);
  }//doPost
}

An example of a browser display for this servlet is shown in Figure 11-3.

Figure 11-3. Finding out a session's creation and last-accessed times
figs/jsjc_1103.gif

As in the prior recipe, this example uses a java.text.DateFormat object to format Date Strings for browser display. The date-related HttpSession methods getCreationTime( ) and getLastAccessedTime( ) return long datatypes, from which java.util.Date objects can be created:

Date creationTime = new Date( session.getCreationTime( ) );

The session's creation time can then be displayed using the DateFormat's format(Date _date) method.

The next recipe shows how a JSP can track session activity.

See Also

Recipe 11.5 and Recipe 11.8; Chapter 1 on web.xml; Chapter 7 of the Servlet v2.3 and 2.4 specifications on sessions; the javax.servlet.http.HttpSession API at http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html; the session-tracking sections of Java Servlet Programming by Jason Hunter (O'Reilly) and JavaServer Pages by Hans Bergsten (O'Reilly).

    [ Team LiB ] Previous Section Next Section