Difference between Servlet and JSP? Example

Servlet and JSP are two of the most popular Java web technologies to generate dynamic content in Java web applications there is some key difference between them. For example, Servlet is designed for Java developers as most of the coding is done in Java, while JSP was designed and developed for web GUI developers e.g. guys who write HTML and JavaScript and that's why you see the coding in JSP is tag-based. Even though you can use HTML tags as String inside Servlet and Java code as Scriptlet inside JSP, both are considered as bad practices and should be avoided at all costs because they are very hard to maintain.

The different nature of Servlet and JSP also allows frontend and backend developers to work in parallel, JSP work is mostly done by frontend developers while Servlet work is done by backend developers.

In the MVC design pattern and framework, the View part is implemented using JSP while Controller is implemented using Servlet. Models are nothing but plain old Java objects. That's also one of the main differences between Servlet and JSP that one is used as a view and the other is used as a controller.   Let's see some more differences to answer this question better.


Difference between Servlet and JSP in Java JEE?

Here are some of the more factual differences between Servlet and JSP. Some of the points are very important to mention e.g. How JSP is translated and compiled into Servlet and how it impacts the response time. Others are more to show that you have good knowledge of the technologies you are using.



1. Usage
The first and foremost difference between Servlet and JSP is that a JSP is a web page scripting language that can generate dynamic content while Servlets are Java programs that are already compiled which also creates dynamic web content.

2. Working
The second difference between Servlet vs JSP is that JSP is actually translated and compiled into Servlet by web container when the first request comes for it, on the other hand, Servlet is already a Java program, whose instance is created and managed by the web container.

3. Performance
Since JSP is translated and compiled into Servlet, they run faster compared to JSP.

4. Purpose
JSP was designed for HTML developer who doesn't know Java but familiar with the tag-based markup language like HTML, while Servlet is designed for Java developers who like to code in Java.



5. MVC
In the MVC design pattern, JSP (Java Server Pages) act as a view and servlet act as a Controller.

6. View
JSP are generally preferred when there is not much processing of data required. But Servlets are best for use when there is more processing and manipulation involved. Your JSP should be as dumb as possible i.e. it should not contain any logic, all logic should go to Servlets. The job of JSP should just be to display the data provided via model to it. If your JSP starts containing logic then it would be difficult to maintain.

7. Custom Tags
The advantage of JSP programming over Servlet is that we can build custom tags that can directly call Java beans. There is no such facility in the servlets. On the other JSTL is the popular tag library which allows you to completely remove Java from JSP. 

By using expression language and the JSTL core tag library, you can make your JSP free of Java. If you want to learn more about the JSTL core tag library I suggest you read Head First Servlet and JSP, it has explained core tags from JSTL
very well.

Difference between Servlet and JSP?



8. Scripting
We can achieve the functionality of JSP at the client-side by running JavaScript at the client-side. There are no such methods for servlets.


That's all about the difference between Servlet and JSP. AS a Java Web developer you should be familiar with Servlet and JSP and have a good understanding of how and where to use them. JSP is generally used in the front end or GUI layer to create views, while Servlet is mostly used in the backend as Controller in MVC pattern whose job is to capture and redirect HTTP requests for further processing. In short, a Servlet is HTML in Java, while a JSP is Java in HTML.



Other Servlet and JSP Interview Questions you may like
  • Top 10 Servlet Interview Questions for Java Programmers (list)
  • Can you declare the constructor inside Servlet? (answer)
  • Difference between constructor and init() method in Servlet? (answer)
  • Difference between sendRedirect() and forward in Servlet? (answer)
  • Difference between HttpSevlet and GenericServlet in Java? (answer)
  • Difference between ServletConfig vs ServletContext? (answer)
  • 5 Books to Learn Servlet and JSP? (books)
  • Difference between include directive and include action in JSP? (answer)

Thanks for reading this interview question so far. If you like this question and explanation then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment. 

9 comments :

Anonymous said...

Thanks Javin. I have been following your post since a month now, they are really helpful. Keep up the good work.

javin paul said...

your welcome @Anonymous, glad that you find my tutorials useful.

Unknown said...

Easily digestible explanation. Cheers.

javin paul said...

Thank you @Edward, glad you like my explanation of Servlet and JSP.

shashi said...

Hi Javin,

I have a question for you, Please provide the answer,

why it is not required to recompile and redepoly jsp app but it is opposite for servets. In case of JSPs if we just save the page and refresh it in browser changes gets reflected.

Please explain if there is any mechanism behind it.

Thanks in advance

javin paul said...

Hello Shashi, This happened because JSP are converted into Servlet at the request time. It is then compiled and loaded by Servlet container, but for Servlet it directly load the class from the WAR file.

Anonymous said...

3) doesn't make sense. It says that JSP is faster than JSP. Is there a typo here somewhere?

Deepak Tiwari said...

Hi! I am mentioning your statement "Even though you can use HTML tags as String inside Servlet and Java code as Scriptlet inside JSP, both are considered as bad practice and should be avoided at all cost because they are very hard to maintain."

If this is the fact - what should be used instead?

Thanks

javin paul said...

Hello Deepak, JSP should be free of logic, your logic hence code should go to controller class and JSP should just render the data provided to it by Controller. This is proven way to create a MVC application.

Post a Comment