table of contents

Source Code Archive

More Servlets and JavaServer Pages

Chapter 2: A Fast Introduction to Basic Servlet Programming

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.

  • Basic template for packageless servlet.
  • Simple packageless servlet that generates plain text output.
  • Simple packageless servlet that generates HTML output.
  • Simple servlet that demonstrates the use of packages. Remember to put this and all remaining Chapter 2 classes in the moreservlets directory.
  • A utility class that simplifies the generation of the DOCTYPE and TITLE. Note: most of the remaining servlets in this chapter use this class.
  • A servlet that uses the ServletUtilities class. If you get "unresolved symbol" errors when compiling this servlet, review Section 1.6, especially the CLASSPATH settings on pages 19 and 20.
  • web.xml. Deployment descriptor illustrating how to set servlet initialization parameters. See Chapter 5 for specific examples of the use of various web.xml elements, including the setting of initialization parameters for both servlets and JSP pages.
  • Simple servlet that reads three parameters from the form data.
  • ThreeParamsForm.html. Form that sends data to the three-params servlet.
  • Same utility class shown earlier. The point here is the filter method that replaces the special HTML characters.
  • Servlet that shows all the request headers sent in the current request.
  • Servlet that takes a search string, number of results per page, and a search engine name, sending the query to that search engine. Illustrates manipulating response status codes. Requires the SearchSpec and ServletUtilities classes.
  • Small class that encapsulates how to construct a search string for a particular search engine.
  • SearchEngines.html. Form that collects data for the search engine servlet.
  • Servlet that sets six cookies: three that apply only to the current session (regardless of how long that session lasts) and three that persist for an hour (regardless of whether the browser is restarted).
  • Servlet that creates a table of the cookies associated with the current page.
  • Same utility class shown earlier. The point here is the getCookieValue and getCookie methods.
  • Cookie that persists 1 year. Default cookie doesn't persist past current session.
  • Simple example of session tracking.
  • Servlet that displays a list of items being ordered. Accumulates them in an ArrayList with no attempt at detecting repeated items. Used to demonstrate basic session tracking.
  • OrderForm.html. HTML form that collects values of the newItem parameter and submits them to the ShowItems servlet.

URLs Referenced in Chapter

More Information


JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA