O'Reilly logo

Special Edition Using Java™ 2 Enterprise Edition by Mark Wutka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A "Hello World" Servlet

A servlet contains a service method that handles incoming requests. A single servlet can handle many different requests. Listing 13.1 shows you the "Hello World" program in servlet form.

Code Listing 13.1. Source Code for HelloWorldServlet.java
 package usingj2ee; 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 for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println("<HTML>"); out.println("<BODY>"); out.println("<H1>Hello ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required