JSP HTTP status code


The formats of HTTP requests and HTTP responses are similar, and both have the following structure:

  • Start with status line + CRLF (carriage return and line feed)

  • Zero or multiple lines of header module+CRLF

  • A blank line, such as CRLF

  • Optional message body such as file, query data, query output

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

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


The status line contains the HTTP version, a status code, and the short message corresponding to the status code.


The following table lists the HTTP status codes and associated messages that may be returned from the server:

## 204 No Content## 205## 206 Partial Content Multiple Choices Moved Permanently Found See Other Not Modified## 305 Use ProxyUnused The requested page has been temporarily moved to a new URL The server does not recognize request The requested page requires a username and passwordThis status code cannot be used yet## 500 Internal Server Error The request is incomplete and the server encountered an unexpected condition 501 Not Implemented The request is incomplete and the server does not provide the required functionality 502 Bad Gateway The request was incomplete and the server accepted an invalid response from the upstream server 503 Service Unavailable The request is incomplete, the server is temporarily restarted or shut down 504 Gateway Timeout Gateway timeout 505 HTTP Version Not Supported The server does not support the specified HTTP version
Status codeMessageDescription
100 Continue Only part of the request is received by the server, but as long as it is not rejected by the server, the client will continue the request
101 Switching Protocols Server switch protocol
200 OK Request confirmed
201 Created Complete on request, new resource is created
202 Accepted The request was accepted but not processed
203 Non-authoritative Information
Reset Content
## 300
A hyperlink table. Users can select a hyperlink and access it. A maximum of 5 hyperlinks are supported 301
The requested page has been moved to the new URL 302
The requested page has been temporarily moved to a new URL 303
The requested page can be found under a different URL 304
## 306
This status code is no longer used, but the status code is retained 307 Temporary Redirect
400 Bad Request
401 Unauthorized
402 Payment Required
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 is unacceptable to the client
407 Proxy Authentication Required A proxy server must be authenticated before requests can be served
408 Request Timeout The request time exceeded the time that the server could wait, and the connection was disconnected
409 Conflict There is a conflict in the request
410 Gone The requested page is no longer available
411 Length Required "Content-Length" is not defined, the server refused to accept the request
412 Precondition Failed The requested precondition was evaluated by the server as false
413 Request Entity Too Large The server refused to accept the request because the requested entity was too large
414 Request-url Too Long The server refused to accept the request because the URL was too long.A large amount of query information often appears when converting a "POST" request into a "GET" request
415 Unsupported Media Type The server refused to accept the request because the media type is not supported
417 Expectation Failed

# Methods for setting HTTP status codes

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

1 2 3
S.N. Method & Description
public void setStatus ( int statusCode )


This method can set any status code. If your response contains a special status code and a document, be sure to call the setStatus method before returning anything with the PrintWriter

public void sendRedirect(String url)


This method generates a 302 response and also generates a

Location header telling the URL a new document
public void sendError(int code, String message)


This method automatically inserts a status code (usually 404) and a short message into the HTML document and sends it back to the client


HTTP status code program example

The following example will send a 407 error code to the browser, and then the browser will tell you "Need authentication!!!".

<html>
<head>
<title>Setting HTTP Status Code</title>
</head>
<body>
<%
   // 设置错误代码,并说明原因
   response.sendError(407, "Need authentication!!!" );
%>
</body>
</html>

Accessing the above JSP page, you will get the following results:

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