Table of contents
1.
Introduction
2.
JSP HTTP Status Code
2.1.
Format of HTTP Request and Response Message
3.
Categories of HTTP status codes
4.
Status Codes
4.1.
HTTP Status Code Example
4.2.
Methods Used to Set the Status Code
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

JSP HTTP Code

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

HTTP stands for HyperText Transfer Protocol. It is an application-level protocol used in distributed, collaborative, hypermedia systems. It is a data communication protocol used to establish communication between client and server.

HTTP is a TCP/IP-based communication protocol used to deliver the data like image files, HTML files, query results, etc., on the web, with the default port being TCP 80. It provides a standardized way for computers to communicate with each other.

This blog will discuss the JSP HTTP status code methods used to set the code, along with some useful examples that will help you understand the concept better.
 

Source: Link

JSP HTTP Status Code

When a browser requests a web page, a lot of information is sent to the webserver. We cannot access this information directly as it travels as a part of the HTTP request header. 

When a web page is requested, it sends information to the server within the HTTP header. This information sent by the browser is stored in the HTTP request header. 

When a request is processed, a response is generated. This response status line consists of a status code, HTTP version, and an associated message. This message is directly associated with the status code, and the server determines the HTTP version.

The status code in JSP is already set to 200 by default, so we do not have to change it explicitly.

 

Format of HTTP Request and Response Message

  • A status line + Carriage Return Line Feed(CRLF) 
  • Zero or more headers + CRLF
  • A CRLF
  • A message body (optional)

 

Syntax:

HTTP/1.1 200 OK
Content-Type: text/html
Header2: 
  (Blank Line)
<!doctype HTML>
<html>
   <head>...</head>
   <body>
      ...
   </body>
</html>

 

In the given syntax, HTTP/1.1 represents the HTTP version, 200 represents the status code, and OK represents the short message associated with the code.

HTTP/1.1 200 OK is known as the status line.

However, 200 is the default code representing that the request is OK. It is not necessary to specify it.

 

Categories of HTTP status codes

  • 100-199 – The client indicates that they should respond with some action.
  • 200-299 – It signifies that the request is successful.
  • 300-399 – They usually include a location header indicating a new address and are used for the files that have been moved.
  • 400-499 – This indicates an error by the client.
  • 500-599 – This indicates an error by the server.

 

Status Codes

Some of the important status codes that might come in handy along with their associated message are as follows:

 

HTTP Status Code Example

Example 1:

<html>
<body>
<%
   response.sendError(407, "Need authentication!!!" );
%>
</body>
</html>

 

Output:

An example shows how to send a 407 error code to the client's browser.

 

Example 2: 

<html>
<head>
<title>HTTP Status Code</title>
</head>
<body>
<%
response.sendError(404, "Page Not Found!!!" );
%>
</body>
</html>

 

Output:

An example shows how to send a 404 error code to the client's browser.

Methods Used to Set the Status Code

  1. void setStatus(int statusCode): This method sets the arbitrary status code for the JSP Page. If the user wants a status code in their response or a document, we use the method setStatus before returning an object. The setStatus method takes an int as an argument.
     
  2. void sendRedirect(String URL): This method generates a 302 response along with the Location header that gives the new or redirected document URL.
     
  3. void sendError(int code, string message): This method sets the status code as integer and an associated message as a string. This is formatted inside an HTML document.
     

You can also read about the JSP Architecture and Lifecycle.

Frequently Asked Questions

Q1: What is the Session state in HTTP?

Ans: Session state is also known as the Stateless state. HTTP is a stateless protocol. In the session state, the client and server only get to know each other during the current request. If the connection is closed and the two computers want to connect again, they need to provide information to each other as a new connection, and the connection is handled as a brand new connection.

 

Q2: What are the different roles of HTTP?

Ans: The HTTP is used mainly to fetch the HTML document and send it to the client. It was designed exquisitely, and it is continuously evolving. New features are being added to it. It has become the most convenient way to quickly and reliably move data to the web.

 

Q3: What are the different types of HTTP content negotiations?

Ans: The various types of content negotiations are as follows:

  • Server-driven Negotiation: When the server-driven negotiation occurs, the best representation for a response is selected by an algorithm located at the server.
     
  • Agent-driven Negotiation: When an agent-driven negotiation occurs, the best representation for a response is selected by a user agent after receiving an initial response from the origin server.
     
  • Transparent Negotiation: It combines both server-driven negotiation and agent-driven negotiation.

 

Key Takeaways

This blog discussed HTTP Status Code and different methods used in the status code with a few examples.

  • HTTP is a TCP/IP-based communication protocol used to deliver the data like image files, HTML files, query results, etc., on the web.
  • When a web page is requested, it passes information to the server in the HTTP header.
  • When the request is processed, the response is generated. The response status line consists of an HTTP version, a status code, and an associated message.

If you are a beginner and interested in learning other fields such as web development or competitive programming. In that case, follow our guided path to get a good grip on DSA concepts.

Live masterclass