The following is referenced from Http://wiki.jikexueyuan.com/project/servlet/http-status-codes.html:
The format of the HTTP request and the format of the HTTP response message are similar and are structured as follows:
An initial state line + CRLF (carriage return + newline is the new line)
0 or more header lines + CRLF
A blank line, that is, a CRLF
- An optional message body, such as file, query data, or query output
For example, the Server's response header information looks as Follows:
http/1.1 okcontent-type:text/htmlheader2: ... HeaderN: ... (Blank line)<!DOCTYPE ...><HTML><Head>...</Head><Body>...</Body></HTML>
The status line includes the HTTP version (http/1.1 in the example), a status code (200 in the example), and a short message corresponding to the status code (ok in the example).
The following HTTP status code and a list of related messages that may be returned from the Web Server:
code: |
message: |
description: |
100 |
Continue |
Only part of the request has been received by the server, but as long as it is not rejected, the client should continue the Request. |
101 |
Switching protocols |
Server Switching Protocol. |
200 |
Ok |
The request was Successful. |
201 |
Created |
The request is complete, and a new resource is Created. |
202 |
Accepted |
The request is accepted for processing, but the processing is Incomplete. |
203 |
Non-authoritative Information |
|
204 |
No Content |
|
205 |
Reset Content |
|
206 |
Partial Content |
|
300 |
Multiple Choices |
A list of links. The user can select a link to go to that Location. Maximum of five addresses |
301 |
Moved Permanently |
The requested page has been transferred to a new url. |
60W |
Found |
The requested page has been temporarily transferred to a new url. |
303 |
See other |
The requested page can be found under another different url. |
304 |
Not Modified |
|
305 |
Use Proxy |
|
306 |
Unused |
Use this code in a previous Version. It is no longer used, but the code is still Preserved. |
307 |
Temporary Redirect |
The requested page has been temporarily transferred to a new url. |
400 |
Bad Request |
The server does not understand the Request. |
401 |
Unauthorized |
The requested page requires a user name and Password. |
402 |
Payment Required |
You cannot use this code yet. |
403 |
Forbidden |
Access to the requested page is Forbidden. |
404 |
Not Found |
The server was unable to find the requested Page. |
405 |
Method not allowed |
The method specified in the request is not Allowed. |
50W |
Not acceptable |
The server only generates a response that is not accepted by the Client. |
50V |
Proxy Authentication Required |
You must use Proxy server authentication before requesting Delivery. |
408 |
Request Timeout |
The request takes longer than the server can wait to time Out. |
409 |
Conflict |
The request could not be completed because of a conflict. |
410 |
Gone |
The requested page is no longer available. |
411 |
Length Required |
"content-length" is Undefined. The server cannot process request information sent by the client without Content-length. |
412 |
Precondition Failed |
The prerequisites given in the request are evaluated by the server as False. |
413 |
Request Entity Too Large |
The server does not accept the request because the request entity is too large. |
414 |
Request-url Too Long |
The server does not accept the request because the URL is too long. Occurs when a "get" request with a long query information is converted to a "post" Request. |
415 |
Unsupported Media Type |
The server does not accept the request because the media type is not Supported. |
417 |
Expectation Failed |
|
500 |
Internal Server Error |
The request was not Completed. The server encountered an unexpected Situation. |
501 |
Not implemented |
The request was not Completed. The server does not support the required Functionality. |
502 |
Bad Gateway |
The request was not Completed. The server received an invalid response from the upstream Server. |
40R |
Service unavailable |
The request was not Completed. The server is temporarily overloaded or crashed. |
504 |
Gateway Timeout |
The Gateway timed Out. |
505 |
HTTP Version not supported |
The server does not support the "http protocol" Version. |
one, the method of setting the HTTP status code:
The following is a method that you can use to set HTTP status codes in a servlet program. These methods are available through the HttpServletResponse Object.
Method |
Description |
public void SetStatus (int StatusCode) |
This method sets an arbitrary status Code. The SetStatus method takes an int (status code) as a parameter. If your response contains a special status code and documentation, make sure to call SetStatus before using PrintWriter to actually return any Content. |
public void Sendredirect (String Url) |
This method generates a 302 response, along with a URL with a new document location Head. |
public void Senderror (int code, String Message) |
The method sends a status code (typically 404), along with a short message that is automatically formatted and sent to the client inside the HTML Document. |
second, HTTP status Code instance:
The following example sends a 407 error code to the client browser, and the browser will show "authentication required!!!" "message.
//Import required Java librariesImportJava.io.*;Importjavax.servlet.*;Importjavax.servlet.http.*;ImportJava.util.*;//Extend HttpServlet class public classShowErrorextendsHttpServlet {//method to handle GET method Request. public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Set error code and Reason.Response.senderror (407, "need authentication!!!" ); } //method to handle POST method Request. public voiddoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, ioexception {doget (request, response); }}
Configure Web. Xml:
<servlet> <Servlet-name>ShowError</Servlet-name> <Servlet-class>Com.jsoft.testservletbasics.ShowError</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>ShowError</Servlet-name> <Url-pattern>/showerror</Url-pattern> </servlet-mapping>
Calling the above servlet now will display the results shown below:
Test Project: HTTPS://GITHUB.COM/EASONJIM/5_JAVA_EXAMPLE/TREE/MASTER/SERVLETBASICS/TEST5
HTTP status code for servlet