9.6 A Servlet That Shows Per-Client Access Counts

9.6 A Servlet That Shows Per-Client Access Counts

Listing 9.1 presents a simple servlet that shows basic information about the client's session. When the client connects, the servlet uses request.getSession either to retrieve the existing session or, if there is no session, to create a new one. The servlet then looks for an attribute called accessCount of type Integer . If it cannot find such an attribute, it uses 0 as the number of previous accesses . This value is then incremented and associated with the session by setAttribute . Finally, the servlet prints a small HTML table showing information about the session.

Note that Integer is an immutable (nonmodifiable) data structure: once built, it cannot be changed. That means you have to allocate a new Integer object on each request, then use setAttribute to replace the old object. The following snippet shows the general approach for session tracking when an immutable object will be stored.

 HttpSession session = request.getSession(); SomeImmutableClass value =   (SomeImmutableClass)session.getAttribute("someIdentifier"); if (value == null) { // No such object already in session   value = new SomeImmutableClass(...); } else {   value = new SomeImmutableClass(calculatedFrom(value)); }  session.setAttribute("someIdentifier", value);  doSomethingWith(value); 

This approach contrasts with the approach used in the next section (Section 9.7) with a mutable (modifiable) data structure. In that approach, the object is allocated and setAttribute is called only when there is no such object already in the session. That is, the contents of the object change each time, but the session maintains the same object reference .

Figures 9-1 and 9-2 show the servlet on the initial visit and after the page was reloaded several times.

Listing 9.1 ShowSession.java
 package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Servlet that uses session tracking to keep per-client  *  access counts. Also shows other info about the session.  */ public class ShowSession extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)       throws ServletException, IOException {     response.setContentType("text/html");     HttpSession session = request.getSession();     String heading;  Integer accessCount =   (Integer)session.getAttribute("accessCount");   if (accessCount == null) {   accessCount = new Integer(0);   heading = "Welcome, Newcomer";   } else {   heading = "Welcome Back";   accessCount = new Integer(accessCount.intValue() + 1);   }  // Integer is an immutable data structure. So, you     // cannot modify the old one in-place. Instead, you     // have to allocate a new one and redo setAttribute.  session.setAttribute("accessCount", accessCount);  PrintWriter out = response.getWriter();     String title = "Session Tracking Example";     String docType =       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +       "Transitional//EN\">\n";     out.println(docType +                 "<HTML>\n" +                 "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +                 "<BODY BGCOLOR=\"#FDF5E6\">\n" +                 "<CENTER>\n" +                 "<H1>" + heading + "</H1>\n" +                 "<H2>Information on Your Session:</H2>\n" +                 "<TABLE BORDER=1>\n" +                 "<TR BGCOLOR=\"#FFAD00\">\n" +                 "  <TH>Info Type<TH>Value\n" +                 "<TR>\n" +                 "  <TD>ID\n" +                 "  <TD>" + session.getId() + "\n" +                 "<TR>\n" +                 "  <TD>Creation Time\n" +                 "  <TD>" +                 new Date(session.getCreationTime()) + "\n" +                 "<TR>\n" +                 "  <TD>Time of Last Access\n" +                 "  <TD>" +                 new Date(session.getLastAccessedTime()) + "\n" +                 "<TR>\n" +                 "  <TD>Number of Previous Accesses\n" +                 "  <TD>" + accessCount + "\n" +                 "</TABLE>\n" +                 "</CENTER></BODY></HTML>");   } } 
Figure 9-1. First visit by client to ShowSession servlet.


Figure 9-2. Twelfth visit to ShowSession servlet. Access count for this client is independent of number of visits by other clients .


Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net