What is the Difference Between JSP and Servlet

The main difference between JSP and Servlet is that the JSP is a web page scripting language that can generate dynamic web content while the servlet is a Java program that is already compiled and used to create dynamic web content.

In a usual web application, the client requests for a web page from the server and the server responds back with the required page. These pages can be either static or dynamic. The content of a static page is already created.  The content of a dynamic page is created at runtime. JSP and Servlet are two methods in Java to create dynamic web pages. JSP is translated and compiled into a servlet by the web container. On the other hand, a servlet is a Java program that is managed by the web container.

Key Areas Covered

1. What is JSP
     – Definition, Functionality
2. What is Servlet
     – Definition, Functionality
3. What is the Relationship Between JSP and Servlet
     – Outline of Association
4. What is the Difference Between JSP and Servlet
     – Comparison of Key Differences

Key Terms

Java, JSP, Servlet, Servlet Tag, Servlet Mapping Tag

Difference Between JSP and Servlet - Comparison Summary

What is JSP

JSP stands for Java Server Pages. They have the file extension.jsp. It is used to create dynamic web pages. The programmers can insert java code in HTML pages by placing the code inside special JSP tags which starts with <% and ends with %>. There are multiple JSP tags for various purposes such as for obtaining data from a database, passing control between pages, sharing information between requests and pages.

Difference Between JSP and Servlet

Figure 1: Life of a JSP File

A JSP file consist of Java code and HTML/ XHTML/ XML. It is built on top of the Java Servlets. Many JSP pages can be combined with servlets to handle business logic.  JSP has access to various powerful Enterprise Java APIs such as JDBC, EJB, JNDI, etc. It is possible to collect input from users via web page forms and present records from database and create web pages dynamically using JSP.

What is Servlet

Servlet is used to create dynamic web pages. Assume that the client requested for a dynamic web page called abc.html. Server does not have it and the request goes to a helper application called a web container. It can be Apache Tomcat, Glassfish, etc. In the web container, there is a special file called a deployment descriptor (web.xml). It maps every request to a corresponding servlet. Therefore, it knows which servlet should be called for the incoming request.

A servlet is a java file which can take a request from the client, process it and provide an HTML file as the response. There are multiple servlets inside the web container. It is also possible to map multiple requests to one servlet. Therefore, all these configurations are included in this special file which is the web.xml file.

The web.xml has two tags: servlet tag and servlet mapping tag. The servlet tag is used to mention the class name while the servlet mapping tag is used to mention the URL pattern. Annotations help to perform this mapping. To create a servlet, the programmer has to create a class and extend HTTP servlet. Servlet accepts the request, processes it and sends it back to the client in HTML format. Likewise, a dynamic page is sent to the client as the response.

Relationship Between JSP and Servlet

  • JSP is translated and compiled into a servlet.

Difference Between JSP and Servlet

Definition

JSP is a web scripting language that helps developers to create dynamic web pages based on HTML, XML or other documents types. Servlet is a server-side Java program module that processes and responds to client requests by implementing the servlet interface. The definition itself explains the basic difference between JSP and Servlet.

Main Focus

Another difference between JSP and Servlet is that the JSP focuses more on displaying information, while the servlet mainly focuses on information processing.

Execution Speed

Also, the JSP executes slower compared to a servlet and it compiles into a Servlet. On the other hand, servlet runs faster than JSP.

Model View Controller (MVC)

One other difference between JSP and servlet is that, in MVC architecture, JSP works as the view while servlet works as the controller.

Complexity

Furthermore, JSP programming is easier compared to a servlet.

Consist of

JSP file consist of Java, HTML, XHTML or XML. Servlet is a fully functioning java code.

Applicable Scenario

Moreover, we can see a difference between JSP and servlet based on the application too. While the JSP is useful in displaying report details, the servlet is useful in processing a submitted form.

Conclusion

The difference between JSP and Servlet is that the JSP is a web page scripting language that can generate dynamic web content while the servlet is a Java program that is already compiled and used to create dynamic web content. In brief, JSP is more preferred when there is not much involvement in data processing while servlet is more suitable when it requires more data processing.

Reference:

1. Introduction to Servlets | Graphical Representation, Telusko, 2 Feb. 2017, Available here.
2. “JSP Overview.” Www.tutorialspoint.com, Tutorials Point, Available here.

Image Courtesy:

1. “JSPLife” By User:Zedlander, User:Sae1962 –  (CC BY-SA 3.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply