• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Differences Between Servlets and JSP

Servlet Vs JSPServlet and JSP (Java Server Page) are the programs that run on java-enabled web servers or application servers. Though both of them extend the functionalities of the web server or application server but, they do differ in many ways. Servlets are simple java programs that developers design to handle the request from the web browser and generate a dynamic response for the same. On the other hand, JSP is a successor of the servlet, as it is also used to generate a dynamic response for the request generated by the web browser.

So, what’s the difference? The basic difference from the coding perspective is that you encapsulate HTML code inside java code in servlet. On the contrary, in JSP, you encapsulate java code inside the HTML.

Servlets are more complex to write as compared to JSP. But servlets execute faster than JSP. Although Servlets and JSP are quite complex technicalities of java, we will try to explore both of them, and finally, we will identify the differences between them.

Content: Servlet and JSP

  1. Comparison Chart
  2. What is Servlet?
    • Life Cycle of Servlets
  3. What is JSP?
    • Life Cycle of JSP
  4. Key Differences
  5. Conclusion

Comparison Chart

Basis for ComparisonServletJSP
BasicServlet is a Java codeJSP is HTML-based code
ExecutionServlets are directly executed on the web serverJSP is first translated to Servlets and then executed on the web server
Execution SpeedServlets execute fasterJSP executes comparatively slower
CodeServlets are difficult to codeJSPs are easy to code
Handling RequestAccepts all protocol requestsAccepts HTTP requests only
PackagesThe developers can import packages only at the top of the servlet programThe developers can import packages anywhere in the program, i.e. top, bottom or end
MVC ArchitectureIn MVC architecture, servlets paly the role of controllerIn MVC architecture, servlets play the role of view for displaying output
UsefulnessServlets are useful when there is more data processingJSPs are useful when there is less or no data processing
Custom TagsServlets do not allow us to create custom tags.JSP allows us to create custom tags with the use of JSP API
Life Cycle Methodinit(), service(), and destroy()jspInit(), _jspService(), jspDestroy()
OverrideIn servlets, you can override the service() methodIn JSP, you can not override the service() method
Session ManagementIn servlets, the session management is, by default, disabled. It is the responsibility of the JS user to enable it during the sessionIn JSPs, session management is enabled explicitly
Business and Presentation LogicIn Servlet, you have to encode both business logic and presentation logic in a single servlet fileIn JSP, you can distinguish between business logic and presentation logic with the help of JavaBeans
ModificationImplementing modification in servlets is a somewhat time-consuming processImplementation modification is less time consuming
Javascript on the client sideIn servlet, there is no method to execute Javascript on the client sideIn JSP, Javascript can be executed on client-side using client validation

What is Servlet?

Servlet is a pure java program deployed on the server side of a web connection. Servlets accept the request from the client, i.e. the web browser process it and generate a dynamic web page as a response to the client request. Let us try to understand Servlet with the help of the code.

import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType(“text/html”);
Out.println(“<html>”);
Out.println(“<body>”);
Out.println(“<h1>”);
Out.println(“Welcome to Server-side  scripting:” +n);
Out.println(“</h1>”);
Out.println(“</body>”);
Out.println(“</html>”);
Out.close();
}
}

The servlets execute on a java-based server such as Apache Tomcat or Glassfish. The execution of the above servlet will result in the web page shown below:

Servlet Result

If we consider the MVC architecture that divides any application into three logical components model, view and controller, then the servlets work as controllers in it.

Servlets significantly perform better than applets and CGI. They are platform-independent as they are written in java. The servlets are secure as the java security manager is present on the server and secures all the resources on the server. In fact, the servlets are free to communicate with applets, databases and other software.

Life Cycle of Servlet

The life cycle of a servlet involves three methods init(), service(), and destroy(). Let us understand the life cycle of a servlet with a scenario and see where the server invokes these methods in the life cycle of the servlet.

Servlet

  1. A user enters a URL into a web browser for which it generates an HTTP request to the appropriate server.
  2. On receiving the request, the web server maps this request to a particular servlet.
  3. The servlet is identified and loaded to address the space of the server.
  4. The web server then invokes the servlet’s init() method to initialize some of the elements of the servlet before serving any request. The server invokes the init() method only once during the life cycle of a servlet.
  5. After initializing the servlets, the server invokes the service() method of the servlet. The service() method processes the HTTP request and generates a corresponding HTTP response. The server invokes the service() method to process every HTTP request.
  6. When the servlet is no longer needed, the server invokes destroy() method to release all the resources engaged by the servlet.

What is JSP?

Java server pages (JSP) is a server-side scripting language. JSP generates a dynamic webpage as a response to the request from a web browser. The web page that JSP generates incorporates two types of content static content, i.e. the HTML code and dynamic content, i.e. the result of Java code embedded in JSP. Although a JSP file includes HTML and Java, the file is saved by the “.jsp” extension. Let us understand JSP with the help of JSP page shown below:

<%@page ContentType = “text/html” pageEncoding = “UTF-8”%>

<%@page import = “java.io.*,  java.until.*” %>

<html>

<head>

<title> JSP Page </title>

</head>

<body>

<h1> <%

Out.println(“Welcome to Server-side  scripting:” +n);

%>

</h1>

</body>

</html>

The JSP page above will result in the web page shown below. The HTML code in JSP takes care of the presentation of the webpage generated, whereas the java code in JSP looks for the business logic. So, in MVC architecture, JSP works as a view that displays the output generated.

JSP Page Result

The JSP file is always deployed on the server side and can be executed only on the java enabled servers such as Apache Tomcat or Glassfish.

Life Cycle of JSP

The life cycle of JSP represents various stages of JSP, right from its creation to its destruction. The life cycle of JSP is the same as that of a servlet with the addition of one or more phases. We can divide the life cycle of JSP into two phases:

JSP page

  1. Translation and Compilation Phase
  2. Execution Phase

Translation and Compilation Phase

Like a servlet, the JSP page cannot directly respond to a client’s (web browser) request. Initially, the server has to translate the JSP page to an equivalent servlet with the help of a JSP translator.

  • The server parses the JPS page to the XML view even before the translation starts.
  • The XML view is validated tag by tag to identify if any syntactic error is present. If an error occurs, the server suspends the translation until the corrections are made.
  • Once errors are corrected, the translation starts.
The translation stage of a JSP page starts due to three reasons.

  1. When the JSP page is requested for the first time.
  2. To implement any kind of modification.
  3. If an equivalent servlet file/class file of that JSP page gets deleted for any reason.

So, what is the process of translating JSP in an equivalent servlet?

Consider that we have to translate a Test.jsp file. Let’s look into the translation steps:

  1. Initially, the JSP translator translates Test.jsp file into an equivalent servlet Test_jsp.java.
  2. Now the Test_jsp.java file is compiled to generate a class file Test_jsp.class. However, the class file is only generated when the JSP page is translated for the first time or to accommodate modifications, if any are made.
  3. The generated class file is loaded into the web container, and the container produces it whenever any client requests for the same JSP page.
  4. The web container creates an instance or object of the class file Test_jsp.class.

Execution Phase

  1. Once the instance is created, the jspInit() method is invoked to initialize a few elements before servicing any request. The jspInit() method is invoked only once during the life cycle of the JSP page. The jspInit() method can be overridden by the page author during the development of the JSP page.
  2. After initialization, the client request is served using _JspService() method; the service method is invoked frequently for every request of the same JSP page. The underscore in _JspService() method indicates that this method cannot be overridden.
  3. The web container invokes the jspDestroy() method when the servlet instance is not required anymore. Just to end the life cycle of the concerned JSP page.

Key Differences Between Servlets and JSP

  1. Servlet is a java program. However, JSP is an HTML-based code.
  2. Servlets are directly executed on the web server as soon as the request from the web browser arrives. On the other hand, to respond to the request arriving from the web browser JSP file is first translated to the servlet, and then it executes on the webserver to generate the required response.
  3. As servlets directly execute on a web server, so they are faster. On the contrary, the JSP requires translation before the execution they are comparatively slower.
  4. Servlets are quite complex to code, as java instructions encapsulate the HTML code several times. However, the JSP are easy to code as there is a kind of separation between HTML code and java code.
  5. Servlets are capable of handling all types of protocol requests, including HTTP. On the other hand, JSP handles HTTP requests only.
  6. If we talk about servlets, the packages must be imported at the top of the servlet program. However, in the case of JSP, the packages can be imported at any moment in the program, i.e. top, bottom, and end.
  7. In MVC architecture (we can divide an application into three main logical components model, view and controller), servlets play the role of controller, whereas JSP plays the role of view to display the output.
  8. Servlets are useful when there is more data processing. On the contrary, JSPs are useful when there is less or no data processing.
  9. Servlets do not allow us to create custom tags, whereas, with JSP, we can create custom tags using JSP API.
  10. The life cycle methods of servlets are init(), service(), and destroy(). On the other hand, the lifecycle methods of JSP are jspInit(), _jspService(), jspDestroy().
  11. In servlets, a developer can override the service method, whereas in JSP developer can not override the service() method.
  12. In servlets, the session management is, by default, disabled, and it is the responsibility of the JS user to enable it during the session. However, in JSP, session management is, by default, enabled.
  13. In servlets, there is no means to separate business and presentation logic. On the contrary, JSP separates business and presentation logic using JavaBeans.
  14. Implementing modification in servlets is quite time-consuming because it involves reloading, recompiling and restarting the server. On the other hand, implementing modification in JSP is not time-consuming as to see the modification, the user just has to click on the refresh button of the browser.
  15. In servlet, there is no method to execute the Javascript on the client side. However, Javascript can be executed on the client side in JSP using client validation.

Conclusion

So this is all about servlets, JSP and the differences between them.

Related Differences:

  1. Difference Between Applet and Servlet in Java
  2. Difference Between CGI and Servlet
  3. Difference Between Static and Dynamic Web Pages
  4. Difference Between Web Browser and Web Server
  5. Difference Between HTTP and HTTPS

Leave a Reply Cancel reply

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

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy