examples.servlets
Class CookieCounterServlet

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--examples.servlets.CookieCounterServlet
All Implemented Interfaces:

java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

 

public class CookieCounterServlet
extends javax.servlet.http.HttpServlet

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 the development shell

  3. Move to the sample directory:
      cd $WL_HOME/samples/server/examples/src/examples/servlets

  4. Build the servlet using ant:
      ant CookieCounterServlet

  5. Start WebLogic Server with the examples configuration.

  6. Make sure that the examplesWebApp is deployed on your server.

  7. Use a web browser to load the following URL:
        www.setgetweb.com:7001/examplesWebApp/CookieCounterServlet
    

  8. 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.

  9. 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.

 

Files

CookieCounterServlet.java
build.xml

 

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.

On HP platforms, the cookie may not expire after 10 seconds.

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

Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved.

See Also:

Serialized Form

 

 

Constructor Summary

CookieCounterServlet()
 

 

Method Summary

 void init(javax.servlet.ServletConfig config)
          Initializes the servlet.
 void javax.servlet.http.HttpServletResponse)">service(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          Implements the service method of the servlet.
 HttpServlet>

Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
 GenericServlet>

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

CookieCounterServlet

public CookieCounterServlet()

 

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.

Specified by:

init in interface javax.servlet.Servlet

Overrides:

init in class javax.servlet.GenericServlet

javax.servlet.ServletException

  javax.servlet.http.HttpServletResponse)">

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

java.io.IOException