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.
- What are JSP HTTP Status Codes?
- Understanding the HTTP Status Codes
- Methods to Set HTTP Status Code
- 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:
- A status line + CRFL (Carriage Return + Line Feed)
- One or more headers + CRLF
- A CRLF
- A message body (optional)
Methods of HTTP Status Code
- public void setStatus(int statusCode): It sets the status code and gives us the message of the status code that has been set.
- public void sendRedirect(String URL): It gives 302 responses along with the location header which gives the URL of the new document.
- 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
- 100-199: In this category, the client indicates that it should respond with some action.
- 200-299: This category indicates that the request is successful.
- 300-399: This category is used for moved files including location header which also indicates new address.
- 400-499: This category indicates the error by the client.
- 500-599: This category indicates the error by the server.
Some Common Status Codes
- 200: It indicates the request is fine.
- 301: It indicates that the request page has been moved permanently to a new URL.
- 304: It indicates that the request is not modified since the last change.
- 400: It indicates that the server did not understand the request.
- 405: It indicates the method specified in the request is not found.
- 500: It provides Internal Server Error which means the request was not completed and the server met an unexpected condition.
- 503: It indicates the Service Unavailable message which means that the server is temporarily overloading or down.
- 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" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title> Status Code</title> </head> <body> <% response.sendError(404, "Page Not Found"); %> </body> </html>
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.