0

I'm trying to understand how the ServletRequest works.

For example: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterNames()

States "Returns an Enumeration of String objects containing the names of the parameters contained in this request"

I have seen an instance of this as such

Enumeration test_enum = request.getParameterNames();
            StringBuilder sb = new StringBuilder();
            while (test_enum.hasMoreElements()) {
                String paramName = cleanString((String)test_enum.nextElement());
                String paramValue = cleanString(request.getParameter(paramName));
                if (alteredValues.containsKey(paramName)) paramValue = alteredValues.get(paramName);
                try {
                    paramValue = URLEncoder.encode(paramValue, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                }
                sb.append("&").append(paramName).append("=").append(paramValue);
            }

So I understand that the goal of this is to find all parameters and list them out in URL format.

The thing I don't understand is how getParameterNames() finds the parameters on the page, does it just look for any element with a name attribute and counts that as a parameter?

What qualifies as a parameter in this case?

  • It gets the parameters from the request. HTTP requests are a known format. "The page" is ancillary; requests don't have to come from a web page. – Dave Newton May 08 '13 at 00:22
  • http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests may be helpful in understanding how web browsers work "under the covers". – BalusC May 08 '13 at 00:34

1 Answers1

1

When you make a HTTP GET or HTTP POST request to a resource on a server, your browser creates a request package. That request package has certain body parts. In 1 of the body parts, it has all the form fields you entered before making a request.

For ex, this image is a sample HTTP GET request package:

enter image description here

In the above image, you can see bookId=1234&author=Tan+Ah+Teck line. This line is the Parameter line. So in the servlet, you can use request.getParameterNames() which will give you the enumeration of all parameters.

Ravi Trivedi
  • 2,270
  • 2
  • 12
  • 19