Show TOC

Retrieving Client InputLocate this document in the navigation structure

Use

Servlets use the methods that the ServletRequest interface provides to retrieve parameters from the request. HTTP servlets use methods of the HttpServletRequest subinterface that add methods for retrieving HTTP specific parameters of the request.

Procedure

Use the following methods:

If you want to

Use

Get the value of a certain request parameter

request.getParameter(String name)

Get the names of all the parameters sent in the request

request.getParameterNames()

Get all the values of a request parameter

request.getParameterValues(String name)

Get the body of the request using input stream (that is, read it as binary data)

request.getInputStream()

Get the body of the request using input reader (that is, read it as formatted data)

request.getReader()

(for HTTP servlet) to get the query information that is encoded in the request URL

request.getQueryString()

(for HTTP servlet) to get the HTTP method of the request

request.getMethod()

For more information about the parameters and the specifics of the above methods, refer to the Servlet 2.5 API documentation.

Note

The request parameters can be encoded in the URL of the request as a query string, or passed as an HTTP request body, or both. You can retrieve HTTP request parameters that are passed as an HTTP body using getParameters methods of the servlet request in the following case only:

  1. The request is an HTTP or HTTPS request.

  2. The HTTP method is POST.

  3. The content type is application/x-www-form-urlencoded .

If the parameters are passed as HTTP request body, you can choose whether to retrieve them using the getParameters methods of the servlet request or to parse them manually. If you have retrieved request parameters using getParameters methods, the request body will no longer be available for reading directly from the request object's input stream. This rule is also valid the other way round.

Note

You retrieve the request content using one of the two methods: request.getReader() or request.getInputStream() . Do not use both methods at the same time, or else an exception will be thrown.

Note

You can set an encoding to the received request using the setCharacterEncoding method. Use this method before reading any data from the request (that is, before getting the input stream or reader object, and before retrieving request attributes). Otherwise, the method will not take effect.