JSP HTTP status code
HTTP request and HTTP response similar format, have the following structure:
- In the status line + CRLF (carriage return line feed) Start
- Zero or more rows head module + CRLF
- A blank line, such as CRLF
- Optional message body like file, query data, query output
For example, a server response header looks like this:
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 a status code corresponding to a short message.
The following table lists the HTTP status codes and messages associated may be returned from the server:
Status Code | news | description |
---|---|---|
100 | Continue | Only part of the request receiving server, but as long as not to be rejected by the server, the client will request the continuation of this |
101 | Switching Protocols | Server switches protocol |
200 | OK | Request is confirmed |
201 | Created | Request complete, a new resource is created |
202 | Accepted | Request is accepted, but not processed |
203 | Non-authoritative Information | |
204 | No Content | |
205 | Reset Content | |
206 | Partial Content | |
300 | Multiple Choices | A hyperlink table, the user can select a hyperlink and visit, maximum support 5 hyperlinks |
301 | Moved Permanently | The requested page has moved to a new URL under |
302 | Found | The requested page temporarily moved to a new URL under |
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 in use, but the status code is reserved |
307 | Temporary Redirect | The requested page temporarily moved to a new URL under |
400 | Bad Request | Server does not recognize the request |
401 | Unauthorized | The requested page needs a username and password |
402 | Payment Required | Unfortunately you can not use this status code |
403 | Forbidden | Prohibit access to the requested page |
404 | Not Found | The server could not find the requested page |
405 | Method Not Allowed | The method specified in the request is not allowed |
406 | Not Acceptable | Client server can only create a response unacceptable |
407 | Proxy Authentication Required | Before requesting the service must be certified by a proxy server |
408 | Request Timeout | Request time exceeds the server can wait, the connection is disconnected |
409 | Conflict | There are contradictions request |
410 | Gone | Requested page is no longer available |
411 | Length Required | "Content-Length" is not defined, the server rejects the request |
412 | Precondition Failed | Prerequisites requested by the server evaluate to false |
413 | Request Entity Too Large | Because the request entity is too large, the server rejects the request |
414 | Request-url Too Long | The server refuses to accept the request, because the URL is too long. More information appears in a lot of queries to "POST" request into a "GET" request attached to |
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 met an unexpected condition |
501 | Not Implemented | Request is not complete, the server does not provide the required functionality |
502 | Bad Gateway | The request is incomplete, the server from the upstream server received an invalid response |
503 | Service Unavailable | The request is incomplete, the server is temporarily shut down or reboot |
504 | Gateway Timeout | Gateway Timeout |
505 | HTTP Version Not Supported | Server does not support HTTP version specified |
Setting HTTP status code method
The following table lists the HttpServletResponse class method used to set the status code:
SN | Method & description |
---|---|
1 | public void setStatus (int statusCode) This method can be set to any status code. If your response contains a status code and a special document, be sure to use PrintWriter return anything before calling the method setStatus |
2 | public void sendRedirect (String url) This method produces a 302 response, while generating a URL Location header tells a new document |
3 | public void sendError (int code, String message) This method will be a status code (usually 404), and a short message automatically inserted HTML document and sends it back to the client |
HTTP status code Program example
The following example will send 407 error code to the browser, 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>
Access over JSP pages, you will get the following results:
You can also try using a different status code, you will get to see what unexpected results.