F IGURE 4.2
FormServlet Response Page.
The code is the same for both the doPost() and doGet() methods. The first executed line calls
the getParameterNames() method for the current request. This method returns an enumerated
list of parameter names. We then iterate over the Enumerator calling getParameter() and
passing it each one of the parameter names; it returns the value of each request parameter.
These tag/value pairs are then passed to the response writer with some HTML formatting tags.
The completed response is then passed back to the client and displayed in the browser.
This example shows just how easy it is to retrieve request parameters in a servlet. Although the
FormServlet works fine for most requests, it does have a flaw. When we chose to use
getParameter() to retrieve our parameter values, we knew there would only be one value per
request parameter. If you need to handle multiple values, you should use the
getParameterValues() method discussed previously.
In this chapter we covered a lot of information. We looked at how servlets parse request para-
meters. We also created an HTML package that encapsulates basic HTML. At this point you
should feel comfortable with how servlets receive information from the client.