All Examples  This Package

Class 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% 

Copy the file "cookie.jpg" to:

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 file by adding (or uncommenting) the following lines:


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.

Constructor Index

 o CookieCounter()

Method Index

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


 o CookieCounter
 public CookieCounter()


 o init
 public void init(ServletConfig config) throws ServletException
Initializes the servlet. Looks for the property "initial" to set the pageCount variable.

init in class GenericServlet
 o service
 public void service(HttpServletRequest req,
                     HttpServletResponse res) throws IOException
Implements the service method of the servlet.

service in class HttpServlet

All Examples  This Package