Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Servlet HTTP status code


May 14, 2021 Servlet


Table of contents


Servlet HTTP status code

HTTP requests and HTTP response messages are in a similar format, structured as follows:

  • Initial status line and carriage return line break (carriage return plus line break)
  • Zero or more header lines plus carriage breaks
  • A blank line, the carriage return line break
  • An optional message body, such as a file, query data, or query output

For example, the server's 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 lines include 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 information describe
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 is successful.
201 Created This request is complete and creates a new resource.
202 Accepted This request is accepted, but this processing is incomplete.
203 Non-authoritative Information
204 No Content
205 Reset Content
206 Partial Content
300 Multiple Choices Link list.Users can choose a link to enter this location.Up to five addresses.
301 Moved Permanently The requested page has been transferred to a new URL.
302 Found The requested page is 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 previous versions.It is no longer used now, but the code is still retained.
307 Temporary Redirect The requested page is temporarily transferred to a new URL.
400 Bad Request The server does not understand the request.
401 Unauthorized The requested page requires a username and password.
402 Payment Required You can't use this code.
403 Forbidden It is forbidden to access the requested page.
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 only generates a response that is not accepted by the client.
407 Proxy Authentication Required Before requesting delivery, you must use the proxy server verification.
408 Request Timeout The time requested is longer than the server waiting for the server, timeout.
409 Conflict Requests cannot be completed because of conflict.
410 Gone The requested page is no longer available.
411 Length Required "Content-Length" is not defined.The server cannot process request information that does not have Content-Length.
412 Precondition Failed The prerequisites given in the request are evaluated by the server to 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.When you convert a "POST" request to a "GET" request with long query information.
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 Unfinished request.The server encountered an unexpected situation.
501 Not Implemented Unfinished request.The server does not support the required features.
502 Bad Gateway Unfinished request.The server receives an invalid response from the upstream server.
503 Service Unavailable Unfinished request.The server is temporarily overloaded or dead.
504 Gateway Timeout Gateway timeout.
505 HTTP Version Not Supported The server does not support the "HTTP Protocol" version.

The method by which the HTTP status code is set

The following methods can be used to set the HTTP status code in a servlet program. These methods are available through the HttpServletResponse object.

Serial number Method & Description
1 public void setStatus ( int statusCode )
This method sets an arbitrary status code.The SetStatus method accepts an int (status code) as a parameter.If your reaction contains a special status code and document, make sure you are using PrintWriter Call SetStatus before returning anything.
2 public void sendRedirect(String url)
This method generates a 302 response, along with a new document URL Location head.
3 public void sendError(int code, String message)
This method sends a status code (usually 404), together with a short message to the client in the HTML document, and sends it to the client.

HTTP status code instance

The following example sends a 407 error code to the client browser, which displays a "Need !!!" message.

// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

// 扩展 HttpServlet 类
public class showError extends HttpServlet {
 
  // 处理 GET 方法请求的方法
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // 设置错误代码和原因
      response.sendError(407, "Need authentication!!!" );
  }
  // 处理 POST 方法请求的方法
  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
     doGet(request, response);
  }
}

The call to the servlet above now shows the following results:

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