table of contents

Source Code Archive

Core Servlets and JavaServer Pages

Second Edition

Volume 1: Core Technologies

Chapter 3: Servlet Basics

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.

  • Starting point for basic servlets. Download and copy/modify this (or a later servlet) rather than typing in your servlets by hand.
  • Example that illustrates the simplest servlet structure. Generates plain text output.
  • Simple packageless servlet that generates HTML.
  • Simple packaged servlet that generates HTML. Be sure to put this (and all future packaged classes from the book) in the coreservlets subdirectory. You should use this subdirectory both in your development location (e.g., C:\Servlets+JSP\coreservlets) and in the deployment location (e.g., .../WEB-INF/classes/coreservlets).
  • Simple packaged servlet that generates HTML and uses a utility class. If you get an "unresolved symbol" error when compiling, double check the directions in Section 2.8.
  • Utility (helper) class used by
  • Example that illustrates the use of the init and getLastModified methods.
  • Servlet that illustrates the importance of synchronization when maintaining data between requests. This example fails to synchronize, and thus suffers from race conditions: two users could get the same user ID.
  • WebClient. This is a standalone application (not a servlet!) that can be used to interactively connect to servers and test servlets, JSP pages, etc. Download all the files, compile it with "javac" (which will compile all the auxiliary files). Then, run it from the command line with "java WebClient", and type in the host, URI, HTTP headers, etc.
  • EchoServer. This is a standalone application (not a servlet) that acts as a simple debugging Web server. For all requests, the server just echoes back a page showing what was sent (HTTP headers and all). See Chapter 19 for the source code.

URLs Referenced in Chapter

More Information


JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA