WebLogic Server 6.0 Code Examples, BEA Systems, Inc.

examples.servlets
Class CookieCounter

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--examples.servlets.CookieCounter
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class CookieCounter
extends javax.servlet.http.HttpServlet

This servlet example demonstrates how to create and retrieve a cookie and how to set a maximum age on a cookie.

The servlet displays how many times it has been visited by all clients since the server was started, and also displays how many times each client has visited in the 10 seconds that have passed since the last visit.

Build the Example

  1. Open a new command shell.

  2. Set up this development shell as described in Setting up your environment for building and running the examples.

  3. Compile the servlet using the following command line:
      $ javac -d %EX_WEBAPP_CLASSES% CookieCounter.java

  4. Start WebLogic Server with the examples configuration.

  5. Copy the file cookie.jpg, located in the samples/examples/servlets directory in your WebLogic Server installation, to the config/examples/applications/examplesWebApp directory. This directory is the location of the publicly-available files of the examplesWebApp.

Configure the Server

Make sure that the examplesWebApp is deployed on your server.

Run the Example

  1. Use a web browser to load the following URL:
    http://localhost:7001/examplesWebApp/CookieCounter
  2. Click reload in your browser (or type Control-R in most browsers) and observe that the numbers on both lines of text increment each time you click reload.

  3. Wait at least 10 seconds and click reload again in your browser. Note that the text of the second line changes, demonstrating that the cookie has expired after 10 seconds. If you click on reload again, the second line restarts the count from 1.

Notes

If you have cookies disabled in your browser, the servlet will not work. For a failsafe way to handle non-cookie-friendly web browsers, see the SessionServlet example.

There's More...

For more information on servlets, see Programming WebLogic HTTP Servlets.

Author:
Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved., Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.

Constructor Summary
CookieCounter()
           
 
Method Summary
 void init(javax.servlet.ServletConfig config)
          Initializes the servlet.
 void service(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          Implements the service method of the servlet.
 
Methods inherited from class javax.servlet.http.HttpServlet
, doDelete, doGet, doOptions, doPost, doPut, doTrace, getLastModified, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CookieCounter

public CookieCounter()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
Initializes the servlet. Looks for the property "initial" to set the pageCount variable.
Overrides:
init in class javax.servlet.GenericServlet

service

public void service(javax.servlet.http.HttpServletRequest req,
                    javax.servlet.http.HttpServletResponse res)
             throws java.io.IOException
Implements the service method of the servlet.
Overrides:
service in class javax.servlet.http.HttpServlet

Documentation is available at
http://e-docs.bea.com/wls/docs60

Copyright © 2000 BEA Systems, Inc. All Rights Reserved.