table of contents
CSAJSP/2

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.

  • ServletTemplate.java. Starting point for basic servlets. Download and copy/modify this (or a later servlet) rather than typing in your servlets by hand.
  • HelloWorld.java. Example that illustrates the simplest servlet structure. Generates plain text output.
  • HelloServlet.java. Simple packageless servlet that generates HTML.
  • HelloServlet2.java. 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).
  • HelloServlet3.java. 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.
  • ServletUtilities.java. Utility (helper) class used by HelloServlet3.java
  • LotteryNumbers.java. Example that illustrates the use of the init and getLastModified methods.
  • UserIDs.java. 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 WebClient.java" (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

Java

JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA

Struts