HTTP status code in JSP

Source: Internet
Author: User

The following is referenced from http://wiki.jikexueyuan.com/project/jsp/http-status-codes.html:

The HTTP request format, like the HTTP response message format, has the following structure:

    • An initial state line +crlf (carriage return + newline, i.e. new line)
    • 0 or more header row +crlf
    • A blank line, that is, a CRLF
    • An optional message body, such as file, query data or query output

For example, a server response header looks like the following:

http/1.1 okcontent-type:text/htmlheader2: ...  HeaderN: ... (Blank line)<!doctype ...><HTML><Head>...</Head><Body>...</Body></HTML>

The status line contains the HTTP version (http/1.1 in the example), the status code (200 in the example), and the short message corresponding to the status codes (OK in the example).

The following is a list of HTTP status codes and related messages that may be returned from the Web server:

Code: message: Description:
100 Continue Only part of the server request has been received, but as long as it is not denied, the client should continue to request
101 Switching protocols The server exchanged the protocol.
200 Ok Request is OK
201 Created The request is complete and the new resource is created
202 Accepted The request was accepted for processing, but the processing was not completed.
203 Non-authoritative Information
204 No Content
205 Reset Content
206 Partial Content
300 Multiple Choices A list of links. The user can select a link and then jump to that location. A maximum of 5 addresses can be selected
301 Moved Permanently The request page has been moved to the new URL
302 Found The request page is temporarily moved to the new URL
303 See other The request page can be found in a different URL
304 Not Modified
305 Use Proxy
60S Unused This code was used in the previous version. It is no longer in use, but the code remains.
307 Temporary Redirect The request page is temporarily moved to the new URL.
400 Bad Request The server did not understand the request.
401 Unauthorized Request page requires user name and password
402 Payment Required You cannot use this code
403 Forbidden Do not allow access to the request page
404 Not Found The server could not find the request page.
405 Method not allowed The method specified in the request is not allowed to be used.
50W Not acceptable The server can only generate a response that is not received by the client.
407 Proxy Authentication Required A proxy server must be validated before this request gets serviced.
408 Request Timeout The request takes longer than the server is ready to wait.
409 Conflict The conflict request cannot be implemented.
410 Gone The request page is no longer available.
411 Length Required "Content-Length" is not defined. The server does not accept the request without it.
412 Precondition Failed The server gives the assumption that the given request evaluates to False.
413 Request Entity Too Large The server does not accept the request because the request entity is too large.
414 Request-url Too Long The server does not accept the request because the URL is too long. This happens when a "POST" request is converted to a "GET" request with very long query information.
415 Unsupported Media Type The server does not accept the request because the media type is not supported.
417 Expectation Failed
500 Internal Server Error The request was not completed. The server encountered an unexpected condition.
501 Not implemented The request could not be completed. The server does not support the required functionality.
502 Bad Gateway The request could not be completed. Server received an invalid response from the upstream server
503 Service unavailable The request could not be completed. The server is temporarily overloaded or paralyzed.
504 Gateway Timeout The gateway timed out.
505 HTTP Version not supported The server does not support the "HHTP protocol" version.

To set the HTTP status code method:

The following methods can be used to set the HTTP status code of the servlet program. With the HttpServletResponse object, these methods are available.

Method Description
public void setStatus (int statusCode) This method sets an arbitrary status code. The SetStatus method takes an int (status code) as a parameter. If the response contains a special status code and document, be sure to call SetStatus before you actually return anything with PrintWriter.
public void Sendredirect (String URL) The method generates a 302 response and a location header to give the URL of the new document.
public void Senderror (int code, String message) This method sends a status code (usually 404) and a short message that is automatically formatted within the HTML document and sent to the client.

HTTP Status Code instance:

The following example will send a 407 error code to the client browser, and the browser will display "need to verify!!!" The message.

<HTML><Head><title>Setting HTTP Status Code</title></Head><Body><%   // Set ErrorCode andreason. Response.senderror (407, "NEED authentication!!!" );%></Body></HTML>

Now call the above code, the JSP will display the result as follows:

Test Project: Https://github.com/easonjim/5_java_example/tree/master/jspbasics/test6

HTTP status code in JSP

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.