5.5. Status CodesUntil now, our servlet examples have not set HTTP response status codes. We've been taking advantage of the fact that if a servlet doesn't specifically set the status code, the server steps in and sets its value to the default 200 "OK" status code. That's a useful convenience when we are returning normal successful responses. However, by using status codes, a servlet can do more with its response. For example, it can redirect a request or report a problem. The most common status code numbers are defined as mnemonic constants (publicfinalstaticint fields) in the HttpServletResponse class. A few of these are listed in Table 5-1. The full list is available in Appendix C, "HTTP Status Codes". Table 5-1. HTTP Status Codes
5.5.1. Setting a Status CodeA servlet can use setStatus() to set a response status code: public void HttpServletResponse.setStatus(int sc) public void HttpServletResponse.setStatus(int sc, String sm) Both of these methods set the HTTP status code to the given value. The code can be specified as a number or with one of the SC_XXX codes defined within Http-ServletResponse. With the single-argument version of the method, the reason phrase is set to the default message for the given status code. The two-argument version allows you to specify an alternate message. Remember, the setStatus() method should be called before your servlet returns any of its response body. If a servlet sets a status code that indicates an error during the handling of the request, it can call sendError() instead of setStatus(): public void HttpServletResponse.sendError(int sc) public void HttpServletResponse.sendError(int sc, String sm) A server may give the sendError() method different treatment than setStatus(). When the two-argument version of the method is used, the status message parameter may be used to set an alternate reason phrase or it may be used directly in the body of the response, depending on the server's implementation. 5.5.2. Improving ViewFile Using Status CodesSo far, we haven't bothered calling any of these methods to set a response's status code. We've simply relied on the fact that the status code defaults to SC_OK. But there are times when a servlet needs to return a response that doesn't have the SC_OK status code--when the response does not contain the requested data. As an example, think back to how the ViewFile servlet in Chapter 4, "Retrieving Information", handled the FileNotFoundException: // Return the file try { ServletUtils.returnFile(file, out); } catch (FileNotFoundException e) { out.println("File not found"); } Without setting a status code, the best this servlet can do is write out an explanation of the problem, ironically sending the explanation as part of a page that is supposed to contain the file's contents. With status codes, however, it can do exactly what Sun's FileServlet does: set the response code to SC_NOT_FOUND to indicate that the requested file was not found and cannot be returned. Here's the improved version: // Return the file try { ServletUtils.returnFile(file, out); } catch (FileNotFoundException e) { res.sendError(res.SC_NOT_FOUND); } The full effect of a sendError() call is server dependent, but for the Java Web Server this call generates the server's own "404 Not Found" page, complete with Duke's picture (as shown in Figure 5-2). Note that this page is indistinguishable from every other Java Web Server "404 Not Found" page. The call to sendError() also results in a note in the server's access log that the file could not be found. Figure 5-2. The Java Web Server "404 Not Found" pageCopyright © 2001 O'Reilly & Associates. All rights reserved. |
|