May 12, 2021 JSP
HTTP requests are similar in format to HTTP responses and have the following structure:
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 short message corresponding to the status code.
The following table lists the HTTP status codes that may be returned from the server and the messages associated with them:
The status code | News | Describe |
---|---|---|
100 | Continue | Only a portion of the request is received by the server, but the client continues the request as long as it is not rejected by the server |
101 | Switching Protocols | The server switch protocol |
200 | Ok | The request is confirmed |
201 | Created | The request is complete and a new resource is created |
202 | Accepted | The request was accepted but not processed |
203 | Non-authoritative Information | |
204 | No Content | |
205 | Reset Content | |
206 | Partial Content | |
300 | Multiple Choices | A hyperlink table where the user can select a hyperlink and access it, with a maximum support of 5 hyperlinks |
301 | Moved Permanently | The requested page has been moved under the new URL |
302 | Found | The requested page was temporarily moved under the 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 retained |
307 | Temporary Redirect | The requested page was temporarily moved under the new URL |
400 | Bad Request | The server does not recognize the request |
401 | Unauthorized | The requested page requires a username and password |
402 | Payment Required | This status code cannot be used at this time |
403 | Forbidden | Do not access 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 | The server can only create one response that is not acceptable to the client |
407 | Proxy Authentication Required | A proxy server must be authenticated before the request can be serviced |
408 | Request Timeout | The request time exceeded the time the server could wait and the connection was disconnected |
409 | Conflict | There is a contradiction in the request |
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 prerequisite for the request is 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 that appears when converting a POST request to a GET request occurs |
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 situation |
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 received an invalid response from the upstream server |
503 | Service Unavailable | The request is incomplete and the server is temporarily restarted or shut down |
504 | Gateway Timeout | The gateway timed out |
505 | HTTP Version Not Supported | The server does not support the specified HTTP version |
The following table lists the methods used to set the status code in the HttpServletResponse class:
S.N. | Method . . . |
---|---|
1 |
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 PrintWriter |
2 |
public void sendRedirect(String url)
This method produces a 302 response, while producing a Location header that tells the URL a new document |
3 |
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 |
The next example will send the 407 error code to the browser, which will then tell you "!!!".
<html>
<head>
<title>Setting HTTP Status Code</title>
</head>
<body>
<% // 设置错误代码,并说明原因 response.sendError(407, "Need authentication!!!" ); %>
</body>
</html>
Visit the JSP page above and you will get the following results:
You can also try using other status codes to see if you'll get any unexpected results.