8.5 Using Cookies to Detect First-Time Visitors

8.5 Using Cookies to Detect First-Time Visitors

Suppose that, at your site, you want to display a prominent banner to first-time visitors, telling them to register. But, you don't want to clutter up the display showing a useless banner to return visitors.

A cookie is the perfect way to differentiate first-timers from repeat visitors. Check for the existence of a uniquely named cookie; if it is there, the client is a repeat visitor. If the cookie is not there, the visitor is a newcomer, and you should set an outgoing "this user has been here before" cookie.

Although this is a straightforward idea, there is one important point to note: you cannot determine if the user is a newcomer by the mere existence of entries in the cookie array. Many beginning servlet programmers erroneously use the following approach.

 
 Cookie[] cookies = request.getCookies(); if (cookies == null) {   doStuffForNewbie();        // Correct. } else {   doStuffForReturnVisitor(); // Incorrect. } 

Wrong! Sure, if the cookie array is null , the client is a newcomer (at least as far as you can tellhe could also have deleted or disabled cookies). But, if the array is non- null , it merely shows that the client has been to your site (or domainsee setDomain in the next section), not that they have been to your servlet . Other servlets, JSP pages, and non-Java Web applications can set cookies, and any of those cookies could get returned to your browser, depending on the path settings (see setPath in the next section).

Listing 8.1 illustrates the correct approach: checking for a specific cookie. Figures 8-3 and 8-4 show the results of initial and subsequent visits .

Listing 8.1 RepeatVisitor.java
 package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Servlet that says "Welcome aboard" to first-time  *  visitors and "Welcome back" to repeat visitors.  *  Also see RepeatVisitor2 for variation that uses  *  cookie utilities from later in this chapter.  */ public class RepeatVisitor extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)       throws ServletException, IOException {     boolean newbie = true;  Cookie[] cookies = request.getCookies();   if (cookies != null) {   for(int i=0; i<cookies.length; i++) {   Cookie c = cookies[i];   if ((c.getName().equals("repeatVisitor")) &&  // Could omit test and treat cookie name as a flag  (c.getValue().equals("yes"))) {   newbie = false;   break;   }   }   }  String title;     if (newbie) {  Cookie returnVisitorCookie =   new Cookie("repeatVisitor", "yes");   returnVisitorCookie.setMaxAge(60*60*24*365); // 1 year   response.addCookie(returnVisitorCookie);  title = "Welcome Aboard";     } else {       title = "Welcome Back";     }     response.setContentType("text/html");     PrintWriter out = response.getWriter();     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" +                 "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +                 "</BODY></HTML>");   } } 
Figure 8-3. First visit by a client to the RepeatVisitor servlet.

graphics/08fig03.jpg

Figure 8-4. Subsequent visits by a client to the RepeatVisitor servlet.

graphics/08fig04.jpg



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