Servlet vs JSP: What's the Difference?

What Is Servlet?

A servlet is a Java technology that is managed by a container called a servlet engine. It generates dynamic content and interacts with the client through Request and Response. Servlet extends the functionality of a web server.

Though servlets can respond to many types of requests, they generally implement web containers for hosting any website on web server. Therefore, it is qualified as a server-side servlet web API.

In this Servlet Vs. JSP tutorial, you will learn:

What Is JSP?

JSP is a collection of technologies developed by Sun Microsystems. It is used to develop web pages by inserting Java code into the HTML pages by making special JSP tags. It can consist of either HTML or XML (combination of both is also possible) with JSP actions and commands. The full form of JSP is Java Server Pages.

KEY DIFFERENCES

  • Servlet can accept all protocol requests, including HTTP, while JSP can only accept HTTP requests.
  • In MVC architecture, servlet works as a controller while JSP works as a view for displaying output.
  • Servlet should be used when there is more data processing involved whereas, JSP is generally used when there is less involvement of data processing.
  • Servlets run faster than JSP, on the other hand JSP runs slower than servlet as it takes time to compile the program and convert into servlets.
  • You can override the service() method in servlet but, in JSP, you can't override the service() method.
  • In Servlet, you have to implement both business logic and presentation logic in the single file. Whereas in JSP, business logic is split from presentation logic using JavaBeans.

Why use Servlet?

Here, are the reasons of using Servlet

Why use JSP?

Here, are reasons of using JSP:

Servlet Life Cycle

Servlet life cycle is depicted in the below diagram:

Servlet Life Cycle

1) Start: Execution of servlet begins.

2) Loading & instantiation void init(): It is called when servlet is first loaded. This method lets you initialize servlet.

3) Initialized void service(): The purpose of this method is to serve a request. You can call it as many times as you like.

4) Handling request and destroying servlet: Java application must be first determined what code is needed to execute the request URL to provide a response. To destroy servlet Void destroy method is used at the end of servlet life cycle.

5) End of Request Thread: When service() finishes its task, either the thread ends or returns to the thread pool that is managed by servlet contaier.

6) End: Servlet lifecycle finishes.

7): Stop: Servlet stop executing.

JSP Life Cycle

JSP Lifecycle is depicted in the diagram below.

JSP Life Cycle
  1. Translation of JSP page into a servlet.
  2. Compilation of JSP page(Compilation of JSP page into _jsp.java)
  3. Classloading (_jsp.java is converted to class file _jsp.class)
  4. Instantiation(Object of generated servlet is created)
  5. Initialisation(_jspinit() method is invoked by container)
  6. Request Processing(_jspservice() method is invoked by the container)
  7. Destroy (_jspDestroy() method invoked by the container)

Difference between a Servlet and JSP

Here are the main differences between Servlet and JSP:

Servlet JSP
Servlets run faster than JSP. JSP runs slower than servlet as it takes time to compile the program and convert into servlets.
It is hard to write code in servlet. It's easier to code in JSP compared to servlets.
In MVC architecture, servlet works as a controller. In MVC architecture, JSP works as a view for displaying output.
It should be use when there is more data processing involved. JSP is generally used when there is no involvement of much data processing.
There is no custom tag writing facility in servlets. You can easily build custom tags that can directly call Java beans.
Servlet is a java code. JSP is a HTML-based code.
It can accept all protocol requests, including HTTP. It can only accept HTTP requests.
You can override the service() method. In JSP, you can't override the service() method.
In Servlet, by default, session management is not enabled, user has to enable it explicitly. In JSP, session management is automatically enabled.
In Servlet, you have to implement both business logic and presentation logic in the single file. In JSP, business logic is split from presentation logic using JavaBeans.
Modification in Servlet file is a time consuming due to reloading, recompiling, and restarting the server. JSP modification is fast, as you just need to click one refresh button.

Advantages of Servlet

Here are benefits/ pros of using servlet:

Disadvantages of Servlet

Here are cons/drawbacks for using servlet:

Advantages of JSP

Here, are benefits/ pros of using JSP

Disadvantages of JSP

Here are cons/drawbacks for using JSP:

 

YOU MIGHT LIKE: