Difference Between Servlet and JSP



Both Servlets and JSP are used in web development. The Servlets handle the logic (processing our requests and interacting with databases), and JSPs handle the presentation (displaying dynamic content on the web page). By separating the logic and presentation, our web applications become more manageable and scalable.

In brief, we can think of Servlets as Java programs that run on a web server. They work as the middle layer between a request from an HTTP client (like a browser) and databases or applications on the server. When we send a request to a server, the Servlet processes that request, and if needed it interacts with the database or application. And, after that, whatever response it might get is sent back to the user.

On the other hand, JSP (also known as JavaServer Pages) is a text document that combines two types of text: static text like HTML and dynamic text generated after the server processes the request.

Example of Servlet

From the following examples, we can see that the output of both Servlet and JSP is the same. The main difference between Servlets and JSPs is how they are implemented. Servlet are Java programs that runs on a web server. JSP is just a normal text file that combines regular content (like HTML) with content that comes from Java code in other words static and dynamic content.

JavaTester.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JavaTester extends HttpServlet {
   private String message;
   public void init() throws ServletException {
      // Do required initialization
      message = "Hello World";
   }
   public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // Set response content type
      response.setContentType("text/html");
      // Actual logic goes here.
      PrintWriter out = response.getWriter();
      out.println(message);
   }
}

Output

Hello World

Example of JSP

index.jsp

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

Output

Hello World

Servlet vs JSP

The following are the important differences between Servlet and JSP - 

Key Servlet JSP
Implementation Servlet is developed in Java language.

JSP is primarily written in HTML, but it can also include Java code. Although you can write Java code directly in JSP, we have to use JSTL (JSP Standard Tag Library).

MVC In contrast to MVC, we can state servlet as a controller that receives the request process and sends back the response. On the other hand, JSP plays the role of view to render the response returned by the servlet.
Request type Servlets can accept and process all type of protocol requests. JSP on the other hand is compatible with HTTP requests only.
Session Management In Servlet, session management is not enabled by default, the user has to enable it explicitly. On the other hand in JSP, session management is automatically enabled.
Performance Servlet is faster than JSP. JSP is slower than Servlet because the translation of JSP to Java code occurs first followed by compilation.
Modification reflected Modification in Servlet is a time-consuming task because it includes reloading, recompiling, and restarting the server as we make any change in our code to get reflected. On the other hand, modifying JSP is fast because you just need to click the refresh button for the code changes to take effect.
Updated on: 2024-11-14T10:41:35+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements