Edit Edit

Java Servlet FAQ


How do you retrieve a request parameter?


public void doGet(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException
{
    String name = req.getParameter("nameparam");
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException, IOException
{
    res.setContentType("text/plain");

    String[] names = req.getParameterValues("nameparam");

    // echo the names back to the client
    PrintWriter out = res.getWriter()
    for (int i=0; i<names.length; i++) {
        out.println("Name" + i + ": " + names[i]);
    }
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException, IOException
{
    res.setContentType("text/plain");

    Enumeration enum = req.getParameterNames();

    // echo the parameter names and values back to the client
    PrintWriter out = res.getWriter()
    while (enum.hasMoreElements()) {
        String name = (String) enum.nextElement();
        out.println("Parameter Name: " + name +
                    " Value: " + req.getParameter(name));
    }
}



Last updated 12/24/1999 05:59:13 PM