table of contents
CSAJSP/2

Source Code Archive

Core Servlets and JavaServer Pages

Second Edition

Volume 1: Core Technologies


Chapter 8: Handling Cookies

Complete source code for every example from this chapter, available free for unrestricted use. Live links to all URLs cited in the chapter. More information. To view source code for other chapters in this book, mouse over the table of contents bar on the left.

Source Code from Chapter

Note: right-click or shift-click on the links to download the associated file.

  • RepeatVisitor.java. Servlet that says "Welcome aboard" to first-time visitors and "Welcome back" to repeat visitors. Also see RepeatVisitor2 for variation that uses cookie utilities from later in this chapter.
  • CookieTest.java. Servlet that illustrates the difference between session cookies and persistent cookies.
  • CookieUtilities.java. Utility that simplifies retrieving the cookie (or value of cookie) with the designated name.
  • LongLivedCookie.java. Cookie that persists one year. Default Cookie doesn't persist past current browsing session.
  • RepeatVisitor2.java. A variation of the RepeatVisitor servlet that uses the CookieUtilities and LongLivedCookie classes to simplify the code.
  • ClientAccessCounts.java. Servlet that prints per-client access counts. Illustrates how to repeatedly change cookie values.
  • RegistrationForm.java. Servlet that displays an HTML form to collect user's first name, last name, and email address. Uses cookies to determine the initial values of each of those form fields. Uses the CookieUtilities class.
  • RegistrationServlet.java. Servlet that processes a registration form containing a user's first name, last name, and email address. If all the values are present, the servlet displays the values. If any of the values are missing, the input form is redisplayed. Either way, the values are put into cookies so that the input form can use the previous values. Uses the NameBean class.

URLs Referenced in Chapter

More Information

Java

JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA

Struts