Saturday, May 6, 2017

Difference between Servlet and JSP?

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 developer 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 practice and should be avoided at all cost 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 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.

Btw, if you are new into Servlet and JSP then I also suggest you go through a comprehensive course to learn Servlet and JSP in depth. If you need a recommendations then I suggest you check out JSP, Servlet, and JDBC for Beginners: Build a Database App course on Udemy by Chad Darby. One of the hands-on course to learn Servlet and JSP.

Difference between Servlet and JSP

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) 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) 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) Since JSP is translated and compiled into Servlet, they run faster compared to JSP.

4) 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) In MVC design pattern, JSP (Java Server Pages) act as a view and servlet act as a Controller.

6) JSP are generally preferred when there is not much processing of data required. But Servlets are best for use when there are 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 to display the data provided via model to it. If your JSP starts containing logic then it would be difficult to maintain.

7)The advantage of JSP programming over Servlet is that we can build custom tags which can directly call Java beans. There is no such facility in servlets. On the other JSTL is the popular tag library which allows you to completely remove Java from JSP. By using expression language and JSTL core tag library, you can make your JSP free of Java. If you want to learn more about 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) We can achieve the functionality of JSP at the client side by running JavaScript at 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 request for further processing. In short, a Servlet is HTML in Java, while a JSP is Java in HTML.

Further Learning
Spring Framework 5: Beginner to Guru
Java Web Fundamentals By Kevin Jones
JSP, Servlets and JDBC for Beginners: Build a Database App

Other Servlet and JSP Interview Questions you may like
Top 10 Servlet Interview Questions for Java Programmers (list)
Can you declare 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 with your friends and colleagues. If you have any question or feedback then please drop a comment. 


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?


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