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 ... |
Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.