Servlet vs JSP

Servlet vs JSP. In Java, there are two technologies available to develop the server-side web components:- Servlet and JSP. But there are many differences between them. In this tutorial, we will discuss the difference between Servlet and JSP i.e. Servlet vs JSP.

Limitations or Disadvantages of Servlets

Servlet having following limitations or disadvantages,

  • To use Servlet strong Java knowledge is required.
  • It is not suitable for non-Java programmers.
  • It doesn’t support tag based programming.
  • Since we mix up both presentation logic (HTML) and business logic (Java code) together, the code looks very clumsy. Moreover the modification done in one logic affects other logic.
pw.println("<h1 style='color:red; text-align:center'> Hello </h1>" );
  • Placing HTML code in pw.println(-) statements of servlet components is an error-prone process.
  • For every modification done in the source code of the Servlet component, Every time we need to recompile the servlet component and reload the web application.
  • The learning curve of servlet technology is a bit high.
  • We should take care of exception handling explicitly.
  • With servlet-based web application development, productivity is poor because it takes time to write the code.
  • We must place the Servlet component in the private area for code security. 
  • Mapping of the servlet component with the request URL is mandatory.
  • No Implicit objects are given.

The request, response, ServletContext, and e.t.c. are servlet containers created built-in objects, but we can’t access them directly. To access those objects we need to write one or another separate logic. For example, to get access to request, response objects we need to write service(-,-) or doXxx(-,-) methods, to get access to ServletContext objects we need to call getServletContext() method.

ServletContext sc = getServletContext();

Similarly to get access to HttpSession object we need to write,

HttpSession ses = req.getSession();

Implicit objects are the objects that are created by JVM/Container and can be used directly without writing any additional code to access them. Examples of implicit objects in standalone environments are the “this” and “super” keywords. We can use them directly in our application without developing any additional code.

Advantages of JSP Over Servlet

JSP has the following advantages.

  • JSP supports tag-based programming.
  • It gives 9 implicit objects.
  • Strong Java programming knowledge is not required.
  • It is suitable for both Java and non-Java programmers.
  • All features of the servlet can be used in the JSP. Internally every JSP component will be converted into a servlet component.
  • No need for exception handling because the internally generated servlet component for the JSP will take care of it.
  • It allows separating business logic (Java code) from Presentation logic (HTML code).
  • Placing HTML code in JSP is not an error-prone process.
  • We can reduce/avoid Java code in JSP.
  • JSP gives built-in JSP tags and also allows to development of custom tags and to use of third-party supplied tags.
  • JSP is easy to learn and apply because the learning curve is very small compared to Servlet.
  • The modifications done in the JSP page/component will reflect directly without touching the source code. No need to compile the source code and reload the application.
  • JSP components or programs can be placed either in public areas or private areas. When they are placed in a public area, then their configuration in web.xml is optional.
  • JSP can be used alone to develop a total website or can be used in combination with Servlet to develop complex websites.
  • It allows us to integrate with HTML, CSS, JavaScript, Jquery, Angular, etc.
  • It gives the feel of working with HTML tags while working with JSP tags.

Difference between Servlet and JSP (Servlet vs JSP)

We have seen the limitations or disadvantages of servlets, and the advantages of JSP over servlets. Now, let us see tabular differences between servlet and JSP (servlet vs JSP).

ServletJSP
It is completely Java code-based Server-side web technology.It is tags-based server-side web technology.
To use Servlet technology, strong Java knowledge is required, so it is suitable for Java programmers.To use JSP technology, strong Java knowledge is not required, so it is suitable for both Java and non-Java programmers.
To execute Servlet components we need a servlet container.To execute a JSP component we need both JSP and Servlet container.
In Servlet component exception handling must be done explicitly.Exception handling is not required in the JSP technology, because the JSP equivalent servlet will take care of it.
We need to mix Java code (business logic) with HTML code (Persistence logic).It can have separate Java code (business logic) with HTML code (Persistence logic).
Servlets have no implicit objects.JSP gives 9 implicit objects.
We must place Servlet components only in a private area and its configuration in web.xml or using annotation is mandatory.JSP component configuration is optional when it is placed in a public area but it is compulsory when it is placed in a private area.
The modification done in the servlet component source will reflect only after recompilation of the servlet component and reloading of the web application.The modification done in JSP components will reflect directly.
Placing HTML code in the Servlet component is an error-prone process.Placing HTML code in JSP isn’t an error-prone process.
The learning curve of Servlet is more compared to JSP.The learning curve of JSP is small compared to Servlet components.
In Servlet, the programming is traditional Java programming.Here, programming is free-style programming.
Servlet used as a controller component in the MVC approach.JSP is used as the view component for showing output in the MVC approach.
Servlet is faster than JSP.JSP is slower than Servlet because internally JSP will be translated to Servlet.
Servlet can accept all protocol requests.JSP only accepts HTTP requests.
In Servlet by default session management is not enabled, users have to enable it explicitly.In JSP session management is automatically enabled.
Servlets should be used when there is more data processing involved. JSP is generally used when there is data processing is less involvement.

Other Differences

Servlet API latest version is 5.x (5.0 version release on September 07, 2020) and its packages are,

  • jakarta.servlet
  • jakarta.servlet.http
  • jakarta.servlet.annotation
  • jakarta.servlet.descriptor

After changing the JEE name from Java EE to Jakarta EE (see 1, 2, 3 – Feb 2018) the package name “javax” is also changed to “jakarta”.

JSP latest version is 3.x (3.0 version released on October 21, 2020) and its packages are,

  • jakarta.servlet.jsp
  • jakarta.servlet.jsp.tagext
  • jakarta.servlet.jsp.el

To execute Servlet components we need a Servlet Container or Servlet Engine. Whereas to execute a JSP component we need a JSP Container or JSP Engine. The JSP Container internally takes the support of the Servlet Container. 

Every JSP Container or JSP engine provides one JSP page compiler to translate JSP into an equivalent servlet component code. In the Tomcat server:- Servlet Container name is Catalina, and Jsp Container name is Jasper which gives JSP page compiler (jspc).

<Tomcat_home>\lib folder having 

  • servlet-api.jar:- representing servlet API
  • jsp-api.jar:- representing JSP API
  • catalina.jar:- representing servlet container
  • jasper.jar:- representing JSP container

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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