The format of the HTTP request and HTTP response message is similar, and thestructure is as follows:
-
Initial status line + carriage return newline character (carriage return + line feed)
-
Zero or more header lines + carriage return newline character
-
A blank line, that is, the enter newline character
-
An optional message body, such as a file, query data, or query output
For example, the response header for the server is as follows:
HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
(Blank Line)
...
...
The status line includes the HTTP version (HTTP/1.1 in this case), a status code (200 in this case), and a short message corresponding to the status code (OK in this case).
The following is a list of HTTP status codes and related information that may be returned from the Web server:
| Code | Message | Description |
|---|---|---|
| 100 | | 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 | | Server switching protocol. |
| 200 | | The request was successful. |
| 201 | | The request is complete and a new resource is created. |
| 202 | | The request is accepted, but the processing is incomplete. |
| 203 | | |
| 204 | | |
| 205 | | |
| 206 | | |
| 300 | | Link list. The user can select a link to go to that location. Five addressesat the most. |
| 301 | | The requested page has been transferred to a new URL. |
| 302 | | The requested page has been temporarily transferred to a new URL. |
| 303 | | The requested page can be found under a different URL. |
| 304 | | |
| 305 | | |
| 306 | | This code was used in previous versions. It is no longer used, but the code is still retained. |
| 307 | | The requested page has been temporarily transferred to a new URL. |
| 400 | | The server does not understand the request. |
| 401 | | The requested page requires a user name and password. |
| 402 | | You cannot use this code yet. |
| 403 | | Access to the requested page is prohibited. |
| 404 | | The server could not find the requested page. . |
| 405 | | The method specified in the request is not allowed. |
| 406 | | The server generates only one response that is not accepted by the client. |
| 407 | | You must use the authentication of the proxy server before the request can be delivered. |
| 408 | | The request takes longer than the server can wait and times out. |
| 409 | | The request could not be completed because of a conflict. |
| 410 | | The requested page is no longer available. |
| 411 | | “Content-Length” is not defined. The server cannot process the request information sent by the client without Content-Length. |
| 412 | | The prerequisites given in the request are evaluated as false by the server. |
| 413 | | The server does not accept the request because the request entity is too large. |
| 414 | | The server does not accept the request because the URL is too long. Occurs when you convert a “post” request to a “get” request with long query information. |
| 415 | | The server does not accept the request because the media type is not supported. |
| 417 | | |
| 500 | | Outstanding request. The server encountered an unexpected condition. |
| 501 | | Outstanding request. The server does not support the required functionality. |
| 502 | | Outstanding request. The server received an invalid response from the upstream server. |
| 503 | | Outstanding request. The server is temporarily overloaded or crashed. |
| 504 | | The gateway timed out. |
| 505 | | The server does not support the HTTP Protocol version. |
2.9.1. The method of setting HTTP status code ¶
The following method can be used to set the HTTP status code in a Servlet program. These methods are passed through HttpServletResponse Object isavailable.
| Serial number | Method & description |
|---|---|
| 1 | |
| 2 | |
| 3 | |
2.9.2. HTTP status code example ¶
The following example sends the 407 error code to the client browser, which displays “Need authenticating error codes!” News.
// Import necessary Java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/showError")
// Extend HttpServlet Class
public class showError extends HttpServlet {
// Method for handling GET method requests
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set error codes and reasons
response.sendError(407, "Need authentication!!!" );
}
// Method for handling POST method requests
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Now, calling the above Servlet will display the following result:
HTTP Status 407 - Need authentication!!!
type Status report
message Need authentication!!!
description The client must first authenticate itself with the proxy (Need authentication!!!).
Apache Tomcat/5.5.29