Simple Servlet Example

You are now in a position to write your first servlet.

In Listing 12.5, you will generate a complete HTML page that displays a simple text string. This servlet extends the HttpServlet class and overrides the doGet() method.

Listing 12.5. A Servlet Generating a Complete HTML Page
 1: import java.io.*;
 2: import javax.servlet.*;
 3: import javax.servlet.http.*;
 4:
 5: public class HTMLPage extends HttpServlet {
 6:     public void doGet(HttpServletRequest req, HttpServletResponse res) throws
 IOException { 7: res.setContentType ("text/html"); // the content's MIME type 8: PrintWriter out = res.getWriter(); // access the output stream ...

Get Sams Teach Yourself J2EE™ in 21 Days 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.