[ Team LiB ] Previous Section Next Section

Retrieving Form Variables in a Servlet

Up to this point, the discussion has centered around the request object in JSPs. The request object in a JSP is an instance of HttpServletRequest.

In Hour 2, "JavaServer Pages Behind the Scenes," you saw servlets that received requests via the doGet method. This works fine for receiving form data via an HTTP GET request, but it will not work for data sent via an HTTP POST request. There are at least two other ways to receive a request in a servlet. First, you can implement a doPost method, which is invoked only if the browser posts data to the servlet. Second, you can implement the service method, which receives data whether it was sent via a GET or a POST request. It is best to receive form data via the service method, because it lets you send data to the servlet via GET or POST. By implementing only doGet or only doPost, you limit the ways to access the servlet. The service method, just like doGet and doPost, takes an HttpServletRequest and an HttpServletResponse as parameters. The HttpServletRequest is the same object as the request object in a JSP. This means, of course, that you already know how to retrieve form variables in a servlet because you do it the same way you do in a JSP.

Handling GET and POST with doPost


If you extend HttpServlet and want to neatly handle both GETs and POSTs using the same code, you can override doPost this way:

public void doPost(HttpServletRequest req, HttpServletResponse res)
                           throws ServletException, IOException
doGet(req, res);

Listing 3.6 shows a servlet version of the ShowParameters JSP you saw in Listing 3.5. Again, you can test it out by passing parameters directly in the URL.

Listing 3.6 Source Code for ShowParametersServlet.java
package examples;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ShowParametersServlet extends HttpServlet
    public void service(HttpServletRequest request,
        HttpServletResponse response)
        throws IOException
// Tell the Web server that the response is HTML.

        PrintWriter out = response.getWriter();

        out.println("You passed me the following parameters:");

// Find out the names of all the parameters.
        Enumeration params = request.getParameterNames();

        while (params.hasMoreElements())
// Get the next parameter name.
            String paramName = (String) params.nextElement();

// Use getParameterValues in case there are multiple values.
            String paramValues[] =

// If there is only one value, print it out.
            if (paramValues.length == 1)
// For multiple values, loop through them.

                for (int i=0; i < paramValues.length; i++)
// If this isn't the first value, print a comma to separate values.
                    if (i > 0) out.print(',');



The output from ShowParametersServlet is identical to the output from the ShowParameters JSP. In fact, the core part of both programs is the same. The only difference is the code to print out the beginning and ending HTML tags.

    [ Team LiB ] Previous Section Next Section