[ Team LiB ] |
Recipe 11.5 Tracking Session Activity in ServletsProblemYou want to use a servlet to track the creation time and last-accessed time for a session. SolutionUse 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. DiscussionThis 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.
Example 11-7 displays the current time, as well as the session's creation and last- accessed times.
Example 11-7. Calling HttpSession methods in a servletpackage 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 timesAs 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 AlsoRecipe 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 ] |