    All Examples  This Package

# Class examples.servlets.CookieCounter

    java.lang.Object
       |
       +----javax.servlet.GenericServlet
               |
               +----javax.servlet.http.HttpServlet
                       |
                       +----examples.servlets.CookieCounter

public class CookieCounter
extends HttpServlet

This simple servlet demonstrates how to create and retrieve a cookie, and how to set a maximum age on a cookie. It displays how many times it has been visited by all clients since the server was started, and displays how many times each client has visited successively within 10 seconds of each visit.

Compile the servlet using the following line:

        javac -d %SERVLET_CLASSES% CookieCounter.java

Copy the file "cookie.jpg" to:

    doc_root/images/cookie.jpg

Where `doc_root` is the document root of your WebLogic Server. This is usually set to the "public_html" directory in your "myserver" directory. See Setting the document root for more details.

Register the servlet in the `weblogic.properties` file by adding (or uncommenting) the following lines:

        weblogic.httpd.register.cookies=examples.servlets.CookieCounter

Start (or restart) the WebLogic Server, and use a web browser to visit the following URL: http://localhost:7001/cookies

Although this is an unusually short-lived cookie, you may easily see the cookie count up, then expire by reloading the page several times, waiting more than 10 seconds, then reloading the page again. Try turning off cookies in your browser, the servlet no longer works. For a failsafe way to handle non-cookie-friendly web browsers, see the SessionServlet example.

##

CookieCounter()

##

init(ServletConfig)
  Initializes the servlet.
service(HttpServletRequest, HttpServletResponse)
  Implements the service method of the servlet.

##

CookieCounter

     public CookieCounter()

##

init

     public void init(ServletConfig config) throws ServletException

  Initializes the servlet. Looks for the property "initial" to set the pageCount variable.

  Overrides:
    init in class GenericServlet

service

     public void service(HttpServletRequest req,
                         HttpServletResponse res) throws IOException

  Implements the service method of the servlet.

  Overrides:
    service in class HttpServlet

    All Examples  This Package