SERVLETS GENERATING DYNAMIC CONTENT
This section shows how to process form data manage persistent data use init parameters
The next Servlet that we are going to write provides a user interface to a mailing list through HTML forms. A user should be able to enter an email address in a text field and press a button to subscribe to the list or another button to unsubscribe.
The Servlet consists of two major parts: Data managment and client interaction.
The client interaction is handled by two of the standard HttpServlet methods, doGet and doPost.
The doGet method replies to GET requests by sending an HTML page which contains the list of the currently subscribed addresses and the form that is used to subscribe or unsubscribe an address:
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
res.setContentType("text/html"); res.setHeader("pragma", "no-cache"); PrintWriter out = res.getWriter();
for(int i=0; i<addresses.size(); i++) out.print("<LI>" + addresses.elementAt(i));
out.print("Enter your email address: <INPUT TYPE=TEXT NAME=email><BR>"); out.print("<INPUT TYPE=SUBMIT NAME=action VALUE=subscribe>"); out.print("<INPUT TYPE=SUBMIT NAME=action VALUE=unsubscribe>"); out.print("</FORM></BODY></HTML>"); out.close();