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*;
 2: import javax.servlet.*;
 3: import javax.servlet.http.*;
 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 O’Reilly online learning.

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