Chapter 3. Getting Started with Servlets

A "Hello World" Servlet

Servlets are a little more involved than Java Server Pages, but in simple cases they aren't too bad. Listing 3.1 shows you the "Hello World" program in servlet form.

Example 3.1. Source Code for HelloWorldServlet.java

 package usingjsp; import javax.servlet.*; import java.io.*; public class HelloWorldServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web server that the response is HTML response.setContentType("text/html"); // Get the PrintWriter ...

Get Special Edition Using Java™ Server Pages and Servlets now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.