Servlet Requests
When a servlet handles a request, it typically needs specific information about the request so that it can respond appropriately. Most frequently, a servlet retrieves the value of a form variable and uses that value in its output. A servlet may also need access to information about the environment in which it is running. For example, a servlet may need to authenticate the user who is accessing the servlet.
The ServletRequest
and HttpServletRequest
interfaces provide access to this kind of information.
When a servlet is asked to handle a request, the servlet container
passes it a request object that implements one of these interfaces.
With this object, the servlet can determine the actual request (e.g.,
protocol, URL, type), access parts of the raw request (e.g., headers,
input stream), and get any client-specific request parameters (e.g.,
form variables, extra path information). For instance, the getProtocol()
method returns the protocol
used by the request, while getRemoteHost()
returns the name of the
client host. The interfaces also provide methods that let a servlet
get information about the server (e.g., getServername()
, getServerPort()
). As we saw earlier, the
getParameter()
method provides
access to request parameters such as form variables. There is also the
getParameterValues()
method, which returns an array of strings that contains all the values for a particular parameter. This array generally contains only one string, but some HTML form elements ...
Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.