Tag Archives: Request

Getting the http Request Parameters in a Servlet

Introduction

The implicit HttpServlet interface comes with two methods which must be implemented.  These are doGet(HttpServletRequest req, HttpServletResponse res) and doPost(HttpServletRequest req, HttpServletResponse res). The example below shows how to get both.

// See also The HttpServlet

// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGetOrPost(req, resp);
}

// This method is called by the servlet container to process a POST request.
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGetOrPost(req, resp);
}

// This method handles both GET and POST requests.
private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Get the value of a request parameter; the name is case-sensitive
    String name = "param";
    String value = req.getParameter(name);
    if (value == null) {
        // The request parameter 'param' was not present in the query string
        // e.g. http://hostname.com?a=b
    } else if ("".equals(value)) {
        // The request parameter 'param' was
       // present in the query string but has no
       // value
       // e.g. http://hostname.com?param=&a=b
    }

    // The following generates a page showing all
    // the request parameters
    PrintWriter out = resp.getWriter();
    resp.setContentType("text/plain");

    // Get the values of all request parameters
    Enumeration enum = req.getParameterNames();
    for (; enum.hasMoreElements(); ) {
        // Get the name of the request parameter
        name = (String)enum.nextElement();
        out.println(name);

        // Get the value of the request parameter
        value = req.getParameter(name);

        // If the request parameter can appear
        // more than once in the query string,
        // get all values
        String[] values =
        req.getParameterValues(name);

        for (int i=0; i<values.length; i++) {
            out.println("    "+values[i]);
        }
    }
    out.close();
}

 

Saving State in the Servlet Request Object



Introduction

The HTTP protocol wasn’t designed to be statefull. This means that by default it doesn’t remember earlier steps.

The HttpServetRequest Object functions as a kind of coat rack. During the session you can “hang” all kinds of Objects to it, which you can retrieve later on in the Session.

This can be useful, for example when you’re working with a Shopping Cart.

Note that the Request Object is available in both Servlets and JSP’s.

// Save and get a request-scoped value
req.setAttribute("com.mycompany.req-param", "req-value");
Object value = req.getAttribute("com.mycompany.req-param");

// Save and get a session-scoped value
HttpSession session = req.getSession(false);
if (session != null) {
    session.setAttribute("com.mycompany.session-param", "session-value");
    value = session.getAttribute("com.mycompany.session-param");
}

// Save and get an application-scoped value
getServletContext().setAttribute("com.mycompany.app-param", "app-value");
value = getServletContext().getAttribute("com.mycompany.app-param");