HTTP reply status

Source: Internet
Author: User
Tags constant continue reset

When a Web server responds to a request from a browser or other client, its answer typically consists of a status line, several answer headers, a blank line, and a content document. Here is one of the simplest answers:

http/1.1 OK

Content-type:text/plain

Hello World

The status line contains the HTTP version, status code, and a brief description of the status code. In most cases, all answer headers except content-type are optional. However, Content-type is required, which describes the MIME type of the subsequent document. Although most answers contain a single document, there are some that are not included, such as a response to a head request that never comes with a document. There are many status codes that are actually used to identify a failed request that does not contain a document (or contains only a brief error message description).

The servlet can use the status code to implement many functions. For example, you can redirect users to another Web site, and you can indicate that the following document is a picture, PDF file, or HTML file, and you can tell the user that you must provide a password to access the document; In this section we will discuss in detail the meaning of the various status codes and what we can do with the code.

7.2 Setting the status code

As mentioned earlier, the HTTP answer status line contains the HTTP version, status code, and corresponding state information. Because the state information is directly related to the status Code, and the HTTP version is determined by the server, there is only one status code that needs to be set by the servlet.

The servlet setting status code generally uses the HttpServletResponse SetStatus method. The parameter of the SetStatus method is an integer (that is, the status code), but in order for the code to be more readable, you can use constants defined in HttpServletResponse to avoid using integers directly. These constants are named according to the standard state information in HTTP 1.1, all names are prefixed with the SC prefix (the Status code abbreviation) and uppercase, and the spaces are underlined. In other words, the state information corresponding to the status code 404 is "Not Found", and the corresponding constant in HttpServletResponse is named Sc_not_found. But there are two exceptions: the constant corresponding to the status code 302 is named after HTTP 1.0, and 307 has no corresponding constant.

Setting the status code does not always mean that the document will not be returned. For example, although most servers return a 404 response with a simple "File not Found" message, the servlet can also customize this response. However, a custom response should call Response.setstatus before any content is sent through PrintWriter.

While setting the status code generally uses the response.setstauts (int) method, for simplicity, HttpServletResponse provides a dedicated method for two common scenarios: the Senderror method generates a 404 response, Generates a short HTML error message document at the same time; the Sendredirect method generates a 302 answer while indicating the URL of the new document in the location header.

7.3 HTTP 1.1 Status code and what it means

The following table shows the common HTTP 1.1 status codes along with their corresponding state information and meanings.

You should be cautious about using state codes that only have HTTP 1.1 support, because many browsers can only support HTTP 1.0. If you use HTTP 1.1-specific status codes, it's a good idea to check the HTTP version number of the request (via the HttpServletRequest Getprotocol method). Status Code state information meaning

The initial request of Continue has been accepted and the customer should continue to send the remainder of the request. (HTTP 1.1 new)

The switching protocols server converts requests to a different protocol (HTTP 1.1 new)

All OK, the answer document for Get and post requests follows. If you do not set the status code setstatus, the servlet defaults to the 202 status code.

The 201 Created Server has created the document, and the location header gives its URL.

The accepted has accepted the request, but the processing has not yet been completed.

203 Non-authoritative Information The document has returned normally, but some of the answer headers may be incorrect because a copy of the document is being used (HTTP 1.1 new).

204 No Content does not have a new document, the browser should continue to display the original document. This status code is useful if the user periodically refreshes the page and the servlet can determine that the user's document is new enough.

205 The Reset content does not have new content, but the browser should reset what it displays. Used to force the browser to clear form input (HTTP 1.1 new).

206 Partial Content Customer sent a GET request with a range header, and the server completed it (HTTP 1.1 new).

Multiple choices customer-requested documents can be found in multiple locations that are already listed in the returned document. If the server wants to make a preference, it should be indicated in the location answer header.

Moved permanently customer requested document elsewhere, the new URL is given in the location header, and the browser should automatically access the new URL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.