| Previous | Table of Contents | Next |
Later in this chapter, in the HttpServletRequest discussion, an example demonstrates how to access form information using the request object. Because HttpServletRequest is a special type of ServletRequest, the majority of the methods used in that example are from Table 7.3.
| Table 7.3 ServletRequest Methods | |
| METHOD | DESCRIPTION |
|---|---|
| Object getAttribute(String) | Returns the value of the named attribute of the request or null if the attribute does not exist. Attributes can provide arbitrary, server-specific information, like access to SSL configurations. |
| String getCharacterEncoding() | Returns the character set encoding for the input of this request. This information is used by the method getReader to provide the appropriate reader for the requests input. |
| int getContentLength() | Returns the size of the request entity data or -1 if not known. This method is particularly used when a form POST request is made. Content length will indicate the size, in bytes, of the input sent by the form. For CGI programmers, this is equivalent to the CONTENT_LENGTH environmental variable. |
| String getContentType() | Returns the Internet Media Type of the request entity data or null if not known. For CGI programmers, this is equivalent to the CONTENT_TYPE environmental variable. |
| ServletInputStream getInputStream() | Returns an input stream for reading binary data in the request body. If the servlet is expecting textual data, it should use getReader instead. The reader will account for character encodings; the stream will not. |
| String getParameter(String) | After parsing the input, this method returns a string containing the lone value of the specified parameter or null if the parameter does not exist. If the parameter specified contains multiple values, this method will return the first one. |
| Enumeration getParameterNames() | After parsing the input, this method returns the parameter names for this request as an enumeration of strings or, if there are no parameters or the input stream is empty, as an empty enumeration. |
| String[] GetParameterValues( String) | After parsing the input, this method returns the values of the specified parameter for the request as an array of strings or null if the named parameter does not exist. This method is used to access parameters with multiple values. For example, an HTML list might send back multiple selections. |
| String getProtocol() | Returns the protocol and version of the request as a string of the form <protocol>/<major version>.<minor version>. |
| BufferedReader getReader() | Returns a buffered reader for reading text in the request body. This reader will be initialized with the appropriate character encoding to ensure that the data read is correct. |
| String getRealPath(String) | Applies alias rules to the specified virtual path and returns the corresponding real path or null if the translation cannot be performed for any reason. Passing in the value of / will return the document root for this servlet. |
| String getRemoteAddr() | Returns the IP address of the agent that sent the request. For CGI programmers, this is the REMOTE_ADDR environmental variable. |
| String getRemoteHost() | Returns the fully qualified host name of the agent that sent the request. For CGI programmers, this is the REMOTE_HOST environmental variable. |
| String getScheme() | Returns the scheme of the URL used in this requestfor example, http, https, or ftp. |
| String getServerName() | Returns the host name of the server that received the request. For CGI programmers, this is the SEVER_NAME environmental variable. |
| int getServerPort() | Returns the port number on which this request was received. For CGI programmers, this is the SEVER_PORT environmental variable. |
Although the remaining methods are self-explanatory, the getAttribute method deserves further discussion. The getAttribute method is provided by the ServletRequest interface to provide server-specific information to the servlet. Not all servers provide the same attributes. As a result, Sun has documented several attributes that its server may provide and named them in a way that ensures the Sun-specific meaning. These attributes are listed in Table 7.4.
Refer to your servers documentation for a complete list of the attributes that are provided to your servlets.
The server provides the servlet with an object that implements the ServletResponse interface in each call to service. This object provides methods for sending a response to the user in the form of a MIME-typed message. The message can be either text or binary, and like the request, the text provided can be encoded in a number of ways.
The methods provided by ServletResponse are listed in Table 7.5. You will notice immediately that these objects are set up in a way such that more information is provided about a generic request than a generic response. This is because it is up to the servlet to define the response, and in most cases, the response will require only a type, a size, and a body. HttpServlets will have access to more response parameters that correspond to the values in an HTTP header. However, because the servlets service method is passed both objects, it can use the information from the request to compose its response.
| Table 7.4 Sun-Defined Attributes | ||
| ATTRIBUTE NAME | ATTRIBUTE TYPE | DESCRIPTION |
|---|---|---|
| javax.net.ssl .cipher_suite | string | The string name of the SSL cipher suite in use if the request was made using SSL. |
| javax.net.ssl .peer_certificates | array of javax.security.cert .X509Certificate objects | The chain of X.509 certificates that authenticates the client. This is available only when SSL is used with client authentication. |
| javax.net.ssl .session | javax.net.ssl.SSLSession | An SSL session object, if the request was made using SSL. |
| Previous | Table of Contents | Next |