Jsp Http Status Codes
The format of an HTTP request and HTTP response is similar, both having the following structure:
* Starts with a status line + CRLF (Carriage Return Line Feed)
* Zero or more header lines + CRLF
* A blank line, i.e., CRLF
* An optional message body, such as a file, query data, or query output
For example, a server response header looks like this:
HTTP/1.1 200 OK Content-Type: text/html Header2: ......HeaderN: ... (Blank Line)......
The status line contains the HTTP version, a status code, and a short message corresponding to the status code.
The following table lists the HTTP status codes that may be returned from the server and their associated messages:
| **Status Code** | **Message** | **Description** |
| --- | --- | --- |
| 100 | Continue | Only part of the request has been received by the server, but the client should continue with the request as long as it hasn't been rejected by the server. |
| 101 | Switching Protocols | The server is switching protocols. |
| 200 | OK | The request is confirmed. |
| 201 | Created | The request is complete, and a new resource is created. |
| 202 | Accepted | The request has been accepted for processing, but the processing has not been completed. |
| 203 | Non-authoritative Information | |
| 204 | No Content | |
| 205 | Reset Content | |
| 206 | Partial Content | |
| 300 | Multiple Choices | A list of hyperlinks, the user can select one hyperlink to visit. Supports up to 5 hyperlinks. |
| 301 | Moved Permanently | The requested page has been moved to a new URL. |
| 302 | Found | The requested page has been temporarily moved to a new URL. |
| 303 | See Other | The requested page can be found under a different URL. |
| 304 | Not Modified | |
| 305 | Use Proxy | |
| 306 | _Unused_ | This status code is no longer used, but it is reserved. |
| 307 | Temporary Redirect | The requested page has been temporarily moved to a new URL. |
| 400 | Bad Request | The server cannot understand the request. |
| 401 | Unauthorized | The requested page requires a username and password. |
| 402 | Payment Required | _This status code is not yet in use._ |
| 403 | Forbidden | Access to the requested page is forbidden. |
| 404 | Not Found | The server cannot find the requested page. |
| 405 | Method Not Allowed | The method specified in the request is not allowed. |
| 406 | Not Acceptable | The server can only generate a response that the client cannot accept. |
| 407 | Proxy Authentication Required | Authentication with a proxy server is required before the request can be served. |
| 408 | Request Timeout | The request took longer than the server was prepared to wait, and the connection was closed. |
| 409 | Conflict | The request could not be completed due to a conflict. |
| 410 | Gone | The requested page is no longer available. |
| 411 | Length Required | "Content-Length" is not defined, and the server refuses to accept the request. |
| 412 | Precondition Failed | The precondition given in the request evaluated to false by the server. |
| 413 | Request Entity Too Large | The server refuses to accept the request because the request entity is too large. |
| 414 | Request-url Too Long | The server refuses to accept the request because the URL is too long. This often occurs when a "POST" request is converted to a "GET" request with a long query information. |
| 415 | Unsupported Media Type | The server refuses to accept the request because the media type is not supported. |
| 417 | Expectation Failed | |
| 500 | Internal Server Error | The request was incomplete, and the server encountered an unexpected condition. |
| 501 | Not Implemented | The request was incomplete, and the server does not support the functionality required to fulfill the request. |
| 502 | Bad Gateway | The request was incomplete, and the server received an invalid response from the upstream server. |
| 503 | Service Unavailable | The request was incomplete, and the server is temporarily restarting or shutting down. |
| 504 | Gateway Timeout | The gateway timed out. |
| 505 | HTTP Version Not Supported | The server does not support the HTTP version specified in the request. |
* * *
## Methods for Setting HTTP Status Codes
The following table lists the methods in the HttpServletResponse class used to set status codes:
| **S.N.** | **Method & Description** |
| --- | --- |
| 1 | **public void setStatus ( int statusCode )** This method can set any status code. If your response contains a specific status code and a document, make sure to call the setStatus method before returning any content with PrintWriter. |
| 2 | **public void sendRedirect(String url)** This method generates a 302 response along with a _Location_ header telling the URL of a new document. |
| 3 | **public void sendError(int code, String message)** This method sends a status code (usually 404) and a short message, automatically inserting them into an HTML document and sending it back to the client. |
* * *
## HTTP Status Code Program Example
The following example will send a 407 error code to the browser, and the browser will tell you "Need authentication!!!".
Setting HTTP Status Code
Accessing the above JSP page will yield the following result:

You can also try using other status codes to see if you get any unexpected results.
YouTip