examples.servlets
Class CookieCounterServletjava.lang.Object | +--javax.servlet.GenericServlet | +--javax.servlet.http.HttpServlet | +--examples.servlets.CookieCounterServletAll 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
- Open a new command shell.
- Set up the development shell
- Move to the sample directory:
cd $WL_HOME/samples/server/examples/src/examples/servlets
- Build the servlet using ant:
ant CookieCounterServlet
- Start WebLogic Server with the examples configuration.
- Make sure that the examplesWebApp is deployed on your server.
- Use a web browser to load the following URL:
www.setgetweb.com:7001/examplesWebApp/CookieCounterServlet
- 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.
- 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:
Constructor Summary
CookieCounterServlet()
HttpServlet>
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.GenericServlet>
Methods inherited from class javax.servlet.http.HttpServlet doDelete, doGet, doHead, 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
CookieCounterServlet
public CookieCounterServlet()
Method Detail
init
public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletExceptionInitializes 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.IOExceptionImplements the service method of the servlet.
Overrides: service in class javax.servlet.http.HttpServlet
java.io.IOException