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();
}

 

2 thoughts on “Getting the http Request Parameters in a Servlet”

  1. Interesting blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. Thanks

  2. One more thing is that when searching for a good internet electronics store, look for online shops that are consistently updated, always keeping up-to-date with the latest products, the top deals, as well as helpful information on goods and services. This will make certain you are getting through a shop which stays on top of the competition and provides you what you should need to make educated, well-informed electronics expenditures. Thanks for the critical tips I have really learned from your blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.