Difference between JSP and Servlet

JSPs are compiled to servlets, effectively allowing you to produce a servlet by just writing the HTML page, without knowing Java. Of course, in practice, to get anything serious done, you need to use some server-side code in there…

If you write servlets, you need to know Java to write the servlet body, and HTML to write the output.

The real advantage of JSP is the use of special tags that allow you to call Java beans directly, and the ability to produce your own custom tags (in Java) to do stuff that you can’t do with a single call to a Java bean (e.g. inserting a list of items into the HTML, which needs a loop construct).

The idea of JSP is that a development house can put a team of specialists onto developing their web applications. A HTML web page writer/designer can write the JSP without knowing Java, using beans and custom tags written by Java experts.

Unfortunately, custom tags are a bit tedious to produce, and for very simple stuff, like simple loops, or stuff that’s unlikely to get re-used, they can seem like overkill.

So JSP allows you to put real Java directly into the HTML inside special tags.

All this Java code will be executed before the HTML is sent to the client, and is generally used to modify the HTML page.

———————————————–

A very basic difference:

  • Servlet is html in java
  • JSP is java in html

Other diff 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.

Leave a comment