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.
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();
out.print("<HTML><HEAD><TITLE>List
Manager</TITLE></HEAD>");
out.print("<BODY><H3>Members:</H3><UL>");
for(int i=0; i<addresses.size(); i++)
out.print("<LI>" + addresses.elementAt(i));
out.print("</UL><HR><FORM
METHOD=POST>");
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();
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.