6.1 Specifying Status Codes

As just described, the HTTP response status line consists of an HTTP version, a status code, and an associated message. Since the message is directly associated with the status code and the HTTP version is determined by the server, all a servlet needs to do is to set the status code. A code of 200 is set automatically, so servlets don't usually need to specify a status code at all. When they do want to, they use response.setStatus , response.sendRedirect , or response.sendError .

Setting Arbitrary Status Codes: setStatus

When you want to set an arbitrary status code, do so with the setStatus method of HttpServletResponse . If your response includes a special status code and a document, be sure to call setStatus before actually returning any of the content with the PrintWriter . The reason is that an HTTP response consists of the status line, one or more headers, a blank line, and the actual document, in that order . Servlets do not necessarily buffer the document, so you have to either set the status code before using the PrintWriter or carefully check that the buffer hasn't been flushed and content actually sent to the browser.

Core Approach


Set status codes before sending any document content to the client.

The setStatus method takes an int (the status code) as an argument, but instead of using explicit numbers , for readability and to avoid typos, use the constants defined in HttpServletResponse . The name of each constant is derived from the standard HTTP 1.1 message for each constant, all upper case with a prefix of SC (for Status Code ) and spaces changed to underscores. Thus, since the message for 404 is Not Found, the equivalent constant in HttpServletResponse is SC_NOT_FOUND . There is one minor exception, however: the constant for code 302 is derived from the message defined by HTTP 1.0 (Moved Temporarily), not the HTTP 1.1 message (Found).

Setting 302 and 404 Status Codes: sendRedirect and sendError

Although the general method of setting status codes is simply to call response.setStatus(int) , there are two common cases for which a shortcut method in HttpServletResponse is provided. Just be aware that both of these methods throw IOException , whereas setStatus does not. Since the doGet and doPost methods already throw IOException , this difference only matters if you pass the response object to another method.

  • public void sendRedirect(String url)

    The 302 status code directs the browser to connect to a new location. The sendRedirect method generates a 302 response along with a Location header giving the URL of the new document. Either an absolute or a relative URL is permitted; the system automatically translates relative URLs into absolute ones before putting them in the Location header.

  • public void sendError(int code, String message)

    The 404 status code is used when no document is found on the server. The sendError method sends a status code (usually 404) along with a short message that is automatically formatted inside an HTML document and sent to the client.

Setting a status code does not necessarily mean that you omit the document. For example, although most servers automatically generate a small File Not Found message for 404 responses, a servlet might want to customize this response. Again, remember that if you do send output, you have to call setStatus or sendError first .

Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net