Previous Table of Contents Next


HttpServletRequest

HTTP requests are represented by HttpServletRequest objects. These objects implement the ServletRequest interface and are created by the server before it calls the servlet’s service method. The primary function of a request is to provide data to the servlet. This data comes in several forms, including connection information, request information, and the data sent with the request. For example, a form submitted by the user will be represented to a servlet as an HttpServletRequest object. The request can tell the servlet if this was a POST or GET request; the authentication mechanism, if any, used to identify the client; and the data contained in the form.

For accessing the data sent with the request, use the methods provided by the ServletRequest interface, such as getParameter and getReader. The getReader methods return the raw client input; getParameter processes the input into key-value pairs for easy access. You should not mix these two mechanisms, because getParameter has to process all of the data before it can respond and may make the reader unavailable. ServletRequest also defines some of the standard methods for accessing client information such as the client’s IP address. The HttpServletRequest class adds methods to the interface that provide specific HTTP information. These methods are listed in Table 7.11.

Table 7.10 HttpUtils Methods

METHOD DESCRIPTION

StringBuffer getRequestURL( HttpServletRequest) Using information from the request, this method builds the URL that the client uses to access the servlet.

Hashtable parsePostData(int, ServletInputStream) Parses HTML form data as passed using a POST request, stores it in a hash table, and returns the table. Multivalued element names will have arrays for their value in the hash table.

Hashtable parseQueryString(String) Parses a query string, as provided in a GET request, and builds a hash table of key-value pairs, where the values are arrays of strings. If there is only one value for a key, the array is length 1.

Table 7.11 HttpServletRequest Methods

METHOD DESCRIPTION

String getAuthType() Gets the authentication scheme of this request. For standard Web server authentication, this may be “basic.” In this case, a simple username/password scheme is used.

Cookie[] getCookies() Gets the array of cookies found in this request.

long getDateHeader(String) Gets the value of the requested date HTTP header field of this request. The return value is a long integer indicating milliseconds since midnight, January 1, 1970, UTC. Use this long integer to create a Date object.

String getHeader(String) Gets the value of the requested HTTP header field of this request.

Enumeration getHeaderNames() Gets the HTTP header names for this request.

int getIntHeader(String) Gets the value of the specified integer HTTP header field of this request.

String getMethod() Gets the HTTP method (for example, GET, POST, PUT) with which this request was made.

String getPathInfo() Gets any optional extra path information following the servlet path of this request’s URI but immediately preceding its query string. For example, if the servlet called go is accessed by the URL http://server/servlet/go/run/fast, this value will be “/run/fast,” the path information after the servlet request.

String getPathTranslated() Gets any optional extra path information following the servlet path of this request’s URI but immediately preceding its query string and translates it to a real path.

String getQueryString() Gets any query string that is part of the HTTP request URI. This will be a string that is included in the URL of the request after a question mark. For example, the query string http://server/servlet/go?one has the query string “one.”

String getRemoteUser() Gets the name of the user making this request. This value will be empty if no authentication scheme is in place.

String getRequestedSessionId() Gets the session id specified with this request.

String getRequestURI() Gets, from the first line of the HTTP request, the part of this request’s URI that is to the left of any query string.

String getServletPath() Gets the part of this request’s URI that refers to the servlet being invoked.

HttpSession getSession(boolean) Gets the current valid session associated with this request if create is false or, if necessary, creates a new session for the request if create is true.

boolean isRequestedSessionIdFromCookie() Checks whether the session id specified by this request came in as a cookie.

boolean isRequestedSessionIdFromUrl() Checks whether the session id specified by this request came in as part of the URL.

boolean isRequestedSessionIdValid() Checks whether this request is associated with a session that is valid in the current session context.


Previous Table of Contents Next