Thursday, 8 June 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.
Servlet, JSP

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 difference between Servlet and JSP that one is used as view and other is used as a controller.   Let's see some more difference to answer this question better.

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 impact 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.

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.