Chapter 6. Generating the Server Response: HTTP Status Codes
Topics in This Chapter
As we saw in the previous chapter, a request from a browser or other client consists of an HTTP command (usually GET or POST ), zero or more request headers (one or more in HTTP 1.1, since Host is required), a blank line, and, only in the case of POST requests , some query data. A typical request looks like the following.
GET /servlet/ SomeName HTTP/1.1 Host: ... Header2 : ... ... HeaderN : (Blank Line)
When a Web server responds to a request, the response typically consists of a status line, some response headers, a blank line, and the document. A typical response looks like this:
HTTP/1.1 200 OK Content-Type: text/html Header2 : ... ... HeaderN : ... (Blank Line) <!DOCTYPE ...> <HTML> <HEAD>...</HEAD> <BODY> ... </BODY></HTML>
The status line consists of the HTTP version ( HTTP/1.1 in the preceding example), a status code (an integer; 200 in the example), and a very short message corresponding to the status code ( OK in the example). In most cases, the headers are optional except for Content-Type , which specifies the MIME type of the document that follows . Although most responses contain a document, some don't. For example, responses to HEAD requests should never include a document, and various status codes essentially indicate failure or redirection (and thus either don't include a document or include only a short error-message document).
Servlets can perform a variety of important tasks by manipulating the status line and the response headers. For example, they can forward the user to other sites; indicate that the attached document is an image, Adobe Acrobat file, or HTML file; tell the user that a password is required to access the document; and so forth. This chapter summarizes the most important status codes and describes what can be accomplished with them; the following chapter discusses the response headers.