HTTP Status Codes

HTTP Status Codes in JSP

In this article, I am going to discuss HTTP Status Codes in JSP with Examples. Please read our previous article where we discussed JSP Server Response. At the end of this article, you will understand the following pointers.

  1. What are JSP HTTP Status Codes?
  2. Understanding the HTTP Status Codes
  3. Methods to Set HTTP Status Code
  4. HTTP Status Code Example
What are HTTP Status Codes?

The response is generated whenever a request is processed, which consists of an HTTP version, a status code, and a message. The message is directly determined by the server as it is associated with the status code and HTTP version. The status code is set to 200 in JSP which is the default status code so we do not need to set it explicitly.

Format of the HTTP request and HTTP response messages:
  1. A status line + CRFL (Carriage Return + Line Feed)
  2. One or more headers + CRLF
  3. A CRLF
  4. A message body (optional)
Methods of HTTP Status Code
  1. public void setStatus(int statusCode): It sets the status code and gives us the message of the status code that has been set.
  2. public void sendRedirect(String URL): It gives 302 responses along with the location header which gives the URL of the new document.
  3. public void sendError(int code, String msg): It returns the status code with the message which is formatted inside HTML.
Different HTTP Status Code Category
  1. 100-199: In this category, the client indicates that it should respond with some action.
  2. 200-299: This category indicates that the request is successful.
  3. 300-399: This category is used for moved files including location header which also indicates new address.
  4. 400-499: This category indicates the error by the client.
  5. 500-599: This category indicates the error by the server.
Some Common Status Codes
  1. 200: It indicates the request is fine.
  2. 301: It indicates that the request page has been moved permanently to a new URL.
  3. 304: It indicates that the request is not modified since the last change.
  4. 400: It indicates that the server did not understand the request.
  5. 405: It indicates the method specified in the request is not found.
  6. 500: It provides Internal Server Error which means the request was not completed and the server met an unexpected condition.
  7. 503: It indicates the Service Unavailable message which means that the server is temporarily overloading or down.
  8. 505: It indicates that the server does not support the HTTP protocol version.
HTTP Status Code Example in JSP

In this example, we are sending an error message to the JSP page explicitly by using a response object with two parameters, i.e. status code and message.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Status Code</title>
  response.sendError(404, "Page Not Found");

HTTP Status Codes in JSP with Examples

In the next article, I am going to discuss JSP Form Processing. Here, in this article, I try to explain HTTP Status Codes in JSP with Examples. I hope you enjoy this HTTP Status Codes article.

Leave a Reply

Your email address will not be published. Required fields are marked *