Simple Servlet Example

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

Listing 12.5. HTMLPage Servlet
 import*; import javax.servlet.*; import javax.servlet.http.*; public class HTMLPage extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType ("text/html"); // the content's MIME type PrintWriter out = res.getWriter(); // access the output stream out.println ("<HTML>"); out.println ("<HEAD><TITLE>First Servlet</TITLE></HEAD>"); out.println ("<BODY>"); out.println ("<H1>My First Servlet Generated HTML Page</H1>"); ...

Get Sams Teach Yourself J2EE™ in 21 Days, Second Edition now with O’Reilly online learning.

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