Server Response generation: HTTP status code -- httpservletresponse Interface Application

Source: Internet
Author: User

1. Http response format

The response of the Web server to the client generally includes a status line, some response headers, and a blank line and corresponding documents.

1. Status line:

A status line consists of an HTTP version, a status code, and a short message indicating whether the request is understood or satisfied. The HTTP Version is determined by the server. Generally, the status code is automatically set to 200 by the system. You can also use servlet to set the status code. Custom messages.

2. Response Message.

3. Empty rows

4. Documentation

2. How to set the status code

Call the method setting status code before sending any document content to the client program.

Use the setstatus, sedredirect, or senderror method of the httpservletresponse interface to set the status code.

The status code consists of three digits. the first digit defines the response category, and the last two digits do not have a specific category.

Use constants defined in httpservletresponse in servlet to reference the status code.

1. Set any status code: setstatus (INT) Method

Call the setstatus method before using printwriter to actually return the content.

The parameter can be a status code of the int type or a constant defined in httpservletresponse.

2. Set the status code 302 and 404: sendredirect (string URL) and senderror (INT code, string MSG. The two throws an ioexception.

Status Code 302: the browser connects to the new URL. The sendredirect method generates a 302 response and Location header. Before a new URL is placed in the location header, the system automatically converts the relative URL to an absolute URL.

Status Code 404 is used when no document is found on the server. The senderror method sends status code and short information. The information is automatically sent to the user in the HTML document.

Role of three-state code

 

[XML]View plaincopy
  1. 1. Specific Status Codes available in http1.1
  2. 100-199: indicates other actions that the user should take.
  3. 200-299: the request is successful.
  4. 300-399: used for files that have been removed, often including location headers, pointing out new addresses.
  5. 400-499: indicates the error caused by the customer.
  6. 500-599: the error caused by the server.
  7. 2. Important status code used by the servlet to interact with the client.
  8. 100 (continue, continue): indicates whether the client program can send additional documents to the server in subsequent requests. The server uses 100 (SC _continue) to continue, and 417 (SC _expectation_failed) to reject the attachment.
  9. 200 (OK, everything is normal): 200 (SC _ OK) indicates that everything is normal.
  10. 202 (accepted, accepted): 202 (SC _accepted) requests have been accepted, but the processing has not been completed.
  11. 204 (NO content, no new document): 204 (SC _no_content) as there is no new document for display, continue to display the previous document.
  12. 205 (rest content, reset content): 205 (SC _rest_content) indicates there is no new document, but the browser should reset the document view. Used to indicate the browser to clear the form fields.
  13. 301 (moved permanently, permanently moved): 301 (SC _moved_permanently) indicates that the requested file has been moved elsewhere. The new URL of the document is provided in the location response header. the browser should reconnect to the new URL.
  14. 302 (found, found): 302 (SC _moved_permanently ). The difference between 301 and 302 is that 302 regards the URL given in the location header as a non-permanent temporary replacement. The browser automatically reconnects to the URL in the location response header.
  15. 304 (not modified, not changed): 304 (SC _not_modified) indicates whether the cached version is the latest. If yes, the client should use it; otherwise, the server should return the requested document and set the normal status code.
  16. Servlet should not directly use this status code. It should implement the getlastmodified method, which is handled by the default service method.
  17. Difference between 307 and 303: If a 303 response is received, the system continues to redirect the get and post requests. If a 307 response is received, the system continues to redirect the GET request and the POST request does not continue.
  18. 400: indicates that the customer request contains a syntax error.
  19. 401: The table client attempts to access password-protected pages.
  20. 403: indicates that the server rejects the provision of related resources, regardless of whether the server is authorized or not.
  21. 404 :( SC _not_found) there is no way to access resources.
  22. 405: This resource cannot be accessed using the request method.
  23. 415: the server does not know how to process the request for additional file types.
  24. 417: Respond to 100, telling the browser not to accept the attachment.
  25. 500: indicates that the server is in disorder. It is caused by a CGI program or servlet crash or an incorrect format of header is returned.
  26. 501: indicates that the server does not support commands sent by the client program.
  27. 503: the server cannot respond due to maintenance or overload.
  28. 505: indicates that the server does not support the HTTP version given in the request line.

 

4. Reconnect, relocate, and refresh:

Reconnect does not display any intermediate page.

When refreshing, the browser temporarily displays an intermediate page.

Relocation is generated by another servlet or webpage, not the servletb itself.

Relocation:

If you know the destination in advance, you can verify the data before transferring it to the destination. You can use your own website to publish links to websites you want to visit, track user behavior, and collect statistics on visits. After obtaining the relevant information, redirect the user to the website that actually needs to be accessed.

5. Locate and jump to the error page

The sendredirect (string URL) method is used to send the 302 status code to the browser. the browser automatically reconnects to the URL provided by the sendredirect method in the location response header and locates the relevant page.

Use the senderror (response. SC _not_found, string MSG) method to set the output error message.

6. Redirect the user to the servlet on the relevant page based on the browser.

Obtain the User-Agent request header to distinguish different browser types. Use the sendredirect (string URL) method to send the 302 status code to the browser. the browser automatically reconnects to the URL provided by the sendredirect method in the location response header, go to the relevant page.

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.