Request Handling

Servlets are request-driven and have specific capabilities available to them that simplify handling of incoming requests.

Recall that a request to a servlet may consist of several pieces of data (for example, when a form consisting of several fields is filled in and submitted).

When the Web container receives a request intended for a servlet, it encapsulates the incoming data into a ServletRequest object (commonly referred to as the request object) and passes it on as a parameter to the servlet's service method. The servlet can then use the methods available in the ServletRequest interface to query the request object. Some of the queries are contained in the following list:

  • getCharacterEncoding obtains information about the encoding format used for the request.

  • isSecure finds out if the request was made over a secure channel.

  • getParameterNames obtains a list of all parameter names in the request.

  • getRemoteAddr determines the IP address of the client that sent the request.

  • getParameter is used to retrieve the first parameter value associated with a named parameter type.

  • getParameterValues is used to retrieve multiple parameter values associated with a named parameter type.

Several other methods are provided for querying different aspects of the request object. See javax.servlet.ServletRequest[1] for more information. A specialized version, HttpServletRequest, for HTTP based servlet requests is also available. See javax.servlet.http.HttpServletRequest for more information.

[1] If you are new to Java or unsure about this reference, see the “Conventions” section in the Preface of this book.

Figure 10-3 shows a simple usage scenario involving a request object.

Figure 10-3. Using the request object
HttpSession session = request.getSession(true);
:
:
// obtain the values for UserID and password
String loginID = rquest.getParameter ("USERID");
String loginPassword = request.getParameter ("PASSWORD");
:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.19.16