English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSP HTTP Status Codes

The format of HTTP requests and HTTP responses is similar, with the following structure:

  • Followed by the status line+Starts with CRLF (carriage return line feed)

  • Zero or more lines of header modules+CRLF

  • A blank line, such as CRLF

  • Optional message bodies such as files, query data, and query output

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

HTTP/1.1 200 OK
Content-Type: .../html
Header2: ...
...
HeaderN: ...
  (Blank Line)
<!doctype ...>
<html>
<head>.../head>
<body>
...
</body>
</html>

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 by the server and the associated messages:

Status CodeMessageDescription
100 Continue Only a part of the request has been received by the server, but as long as it has not been rejected by the server, the client will continue this request
101 Switching Protocols Server Switching Protocol
200 OK The request has been confirmed
201 Created The request was complete, and a new resource was created
202 Accepted The request has been accepted but not yet processed
203 Non-authoritative Information  
204 No Content  
205 Reset Content  
206 Partial Content  
300 Multiple Choices A hyperlinks table, where users can select a hyperlinks and access, with a maximum support of5hyperlinks
301 Moved Permanently The requested page has been moved to a new URL
302 Found The requested page has temporarily moved to a new URL
303 See Other The requested page can be found at a different URL
304 Not Modified  
305 Use Proxy  
306Unused This status code is no longer in use, but the status code is retained
307 Temporary Redirect The requested page has temporarily moved to a new URL
400 Bad Request The server cannot recognize the request
401 Unauthorized The requested page requires a username and password
402 Payment RequiredThis status code cannot be used at present
403 Forbidden Access to the requested page is prohibited
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 create a response that the client cannot accept
407 Proxy Authentication Required A proxy server must be authenticated before the request is served
408 Request Timeout The request time exceeded the time the server can wait, and the connection was disconnected
409 Conflict The request has contradictions
410 Gone The requested page is no longer available
411 Length Required "Content-Length" is not defined, the server refuses to accept the request
412 Precondition Failed The request's preconditions were evaluated as 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. It often appears when a large amount of query information is attached to the 'POST' request converted to a 'GET' request
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 is incomplete, the server encountered an unexpected situation
501 Not Implemented The request is incomplete, the server does not provide the required functionality
502 Bad Gateway The request is incomplete, the server received an invalid response from the upstream server
503 Service Unavailable The request is incomplete, the server is temporarily restarting or shutting down
504 Gateway Timeout Gateway Timeout
505 HTTP Version Not Supported The server does not support the specified HTTP version

Methods to set HTTP status codes

The following table lists the methods used in HttpServletResponse class to set status codes:

S.N.Method & Description
1public void setStatus ( int statusCode ) This method can set any status code. If your response contains a special status code and a document, make sure to call the setStatus method before returning any content with PrintWriter
2public void sendRedirect(String url) This method produces302response, and at the same time produces a Location header tells the URL a new document
3public void sendError(int code, String message) This method takes a status code (usually 404) and a short message, automatically inserted into the HTML document and sent back to the client

HTTP Status Code Program Example

The following example will send407Error code to the browser, and then the browser will tell you "Need authentication!!!".

<html>
<head>
<title>Setting HTTP Status Code</title>
</head>
<body>
<%
   // Set the error code and explain the reason
   response.sendError(407, "Need authentication!!!");
%>
</body>
</html>

Accessing the above JSP page will result in the following:

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