Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between JSP and servlets and when to use each ?

user-image
Question added by Mohamed Elgarhy , Senior/Lead Java Developer (Analyst) , vodafone egypt
Date Posted: 2016/09/22
Danial Asad
by Danial Asad , Senior System Engineer , Infosys

Basic Difference

Servlet is HTML in Java

JSP is Java with HTML

When to use which one?

When your main concern is manipulating data only then use servlet,

When you concern is to display data properly on Front end then use JSP.

 

Firoz Ahmad
by Firoz Ahmad , Software Engineer , Tata Consultancy Service Limited

Agree to Danial Asad answer.Yes, One the most important difference. Adding to tha, It was difficult for servlet developer to design the page (html) and then stuff them in servlet. To tackle this problem, they came up with this JSP solution. When jsp is compiled, its create a servlet in the backend.

Tarek Ghonamy
by Tarek Ghonamy , CTO , Trans Skills

Servlets are the basis of JSP, a JSP gets translated and compiled by the container into HTTP Servlet, although anything that can be done by JSP can be done by Servlet too but the purpose of JSP is to save you from writing raw HTML to the response stream; so in an MVC architecture the controller is a servlet that forwards the requets to the appropriate JSP (the view) which is resposible for sending the HTML to the browser. hence the servlet should be used for backend developers and JSP for front-end developers.

Affan Hasan
by Affan Hasan , Principal Software Engineer , GFK Etilize

A servlet provides the base for handling the 7 HTTP request methods, while JSP is an extension to servlerts in the sense that it separates the HTML response layout from application data.

Ibraheem Umar
by Ibraheem Umar , Technical Consultant , DXC technology Pvt Ltd

Basic differences are 

 

  • JSP is a webpage scripting language that can generate dynamic content while Servlets are Java programs that are already compiled which also creates dynamic web content
  • Servlets run faster compared to JSP
  • JSP can be compiled into Java Servlets
  • It’s easier to code in JSP than in Java Servlets
  • In MVC, jsp act as a view and servlet act as a controller.
  • 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.
  • The advantage of JSP programming over servlets is that we can build custom tags which can directly call Java beans. There is no such facility in servlets.
  • We can achieve functionality of JSP at client side by running JavaScript at client side. There are no such methods for servlets.

JSPs and Servlets are used to create dynamic pages

JSP is an HTML page with TAG to use and/or run Java properties/code. Servlets are Java classes to create HTML pages; so JSP are used in front-end side of WebApp and Servlet are used in Backend side.

Generally is more simple to design a dynamic page using JSP.

 

kamal kanta Garai
by kamal kanta Garai , Software Developer , Inventla Solutions

Main deference between servlet and jsp is ,if we modify some code on our class using servlet so we need to restart the web server ,every time it will take server restart but in case of jsp there is no such type of problem

Kaustav Pakira
by Kaustav Pakira , Php Developer , Matrix Media Solutions Pvt Ltd

JSP is Java server Pages which allows us to embed Java codes along with UI or Html.Besides Jsp pages are processed as servlets in the background in the web containers like tomcat or other servers to process data.

Where as Servlets can allow Html ui part to be written within Java codes but it makes it complicated So JSP is the advanced version as per approach .

SERVLET                                                                                                      JSP

1.Does not support tag-based programming.                                  1.Support

2.Does not gives the implicit object.                                                 2.Given implicit object

3.Strong java knowledge is required to work with the servlet.       3.Not Required

4.Need servlet container to execute servlet code.                      4.Need JSP container to execute JSP code.

5.Exception Handling is Mandatory.                                              5.optional.

6.Servlet is identified with it's URL pattern.                                        6.jsp is identified with it's name or URL pat.

7.cofiguration of the servlet in web.xml the file is mandatory.                   7.optional

Jsp recommended to use for view and servlet is used for bussiness logic

Ankit Sharma
by Ankit Sharma , Software Engineer , Wipro Technologies

JSP is a webpage scripting language that can generate dynamic content. Servlets are Java programs that are already compiled which also creates dynamic web content. JSP run slower compared to Servlet as it takes compilation time to convert into Java Servlets. It's easier to code in JSP than in Java Servlets.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.