| Previous | Table of Contents | Next |
Like the HttpServletRequest object, HttpServletResponse provides HTTP-specific access to the servlets response. This information includes the ability to set headers, add cookies, encode URLs, send error codes, and redirect the client to another URL. These methods are listed in Table 7.12.
Four of these methods are used to notify the client using status codes. These methods include both sets of the setStatus and sendError methods. The two methods called setStatus are used if no error occurs, but the servlet will not return a document if, for example, a put was successful or a file is temporarily unavailable. The sendError methods also cause the servlet to not return a document, but in that case, the client is told that an error has occurred. In both cases, a status code and optional messages can be sent to the client in place of any other data.
The available status codes are provided as static variables of HttpServletResponse. Some of the more common ones are listed in Table 7.13. The complete list is available in the javax.servlet documentation.
When a servlet encounters a problem from which it cannot recover, it should use the status codes and error codes in Table 7.13 to indicate the error to the client.
The following example demonstrates how a servlet can return non-HTML data. In this case, the servlet returns the data for an image. The motivation of this example was to have a servlet that rotated through a set of banner ads. The ads are stored in a directory, and when the servlet is initialized, it reads the directory for available images. When a request is made, the servlet returns the next image in the list. The directory of images is indicated to the servlet via an initialization/configuration parameter to maximize flexibility. We implemented the getLastModified method to indicate that the resource is always new, always just modified. Because this servlet returns an image, it would normally be used as the SRC value for an IMG tag, but it could be accessed directly. Figure 7.3 shows what it looks like to directly access the ad rotator.
Figure 7.2 PrintEnvServlet results.
AdRotatorServlet extends HttpServlet and, for simplicity, marks itself single threaded by implementing SingleThreadModel. A Vector instance variable called imageFiles is used to keep a list of images; an integer called curIndex is used to keep track of the current image to display. These two variables are combined so that the servlet returns a different image from the list on each request.
| Table 7.12 HttpServletResponse Methods | |
| METHOD | DESCRIPTION |
|---|---|
| void addCookie(Cookie) | Adds the specified cookie to the HTTP header of the response. |
| booleancontainsHeader(String) | Checks whether the response message header has a field with the specified name. |
| String encodeRedirectUrl(String) | Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged. This method is used when the session management system requires URL encoding in place of cookies. |
| String encodeUrl(String) | Encodes the specified URL by including the session ID in it or, if encoding is not needed, returns the URL unchanged. This method is used when the session management system requires URL encoding in place of cookies. |
| void sendError(int) | Sends an error response to the client using the specified status code and a default message. |
| void sendError(int, String) | Sends an error response to the client using the specified status code and a descriptive message. |
| void sendRedirect(String) | Sends a temporary redirect response to the client using the specified redirect location URL. |
| void setDateHeader(String, long) | Adds a field to the response header with the given name and date-valued field. |
| void setHeader(String, String) | Adds a field to the response header with the given name and value. |
| void setIntHeader(String, int) | Adds a field to the response header with the given name and integer value. |
| void setStatus(int) | Sets the status code for this response. |
| void setStatus(int, String) | Sets the status code and message for this response. |
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class AdRotatorServlet extends HttpServlet implements SingleThreadModel
| Table 7.13 Common HTTP Status Codes | |
| HTTPSERVLETRESPONSE STATIC VARIABLE | DESCRIPTION OF STATUS CODE |
|---|---|
| SC_ACCEPTED | Indicates that a request was accepted for processing but was not completed. |
| SC_FORBIDDEN | Indicates that the server understood the request but refused to fulfill it. |
| SC_METHOD_NOT_ALLOWED | Indicates that the method specified is not allowed for that URL. |
| SC_NOT_FOUND | Indicates that the requested resource is not available. |
| SC_NOT_MODIFIED | Indicates that a conditional GET operation found that the resource was available and not modified. |
| SC_OK | Indicates that the request succeeded normally. |
| SC_REQUEST_TIMEOUT | Indicates that the client did not produce a request within the time that the server was prepared to wait. |
| SC_SERVICE_UNAVAILABLE | Indicates that the HTTP server is temporarily overloaded and unable to handle the request. |
| SC_UNAUTHORIZED | Indicates that the request requires HTTP authentication. |
{
Vector imageFiles;
int curIndex;
| Previous | Table of Contents | Next |