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 java.io.*; 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 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.