Difference Between Servlet and JSP

This tutorial post highlights the important difference between JSP and Servlet technologies. Servlet and JSP are the two key Java server side technologies that are building blocks for any of the Java web frameworks. It is important to know the difference between these two technologies. This is one of the fundamental concept that confuses the many beginner Java web developers.

The basic difference is that, JSP is for web page and Servlet is for Java components. Servlet is HTML in Java where as JSP is Java in HTML.

  • Java EE Complete Reference
  • A servlet is like any other java class. You put HTML into print statements like you use System.out or how javascript uses document.write.
  • A JSP technically gets converted to a servlet but it looks more like PHP files where you embed the java into HTML.

Servlet Life Cycle

Servlet Life Cycle

JSP Life Cycle

jsp execution life cycle

Difference Between Servlet and JSP

In this article we will list some of the differences between Servlets and JSP.

A servlet is a server-side program and written purely on Java.JSP is an interface on top of Servlets. In another way, we can say that JSPs are extension of servlets to minimize the effort of developers to write User Interfaces using Java programming.
Servlets run faster than JSPJSP runs slower because it has the transition phase for converting from JSP page to a Servlet file. Once it is converted to a Servlet then it will start the compilation
Executes inside a Web server, such as TomcatA JSP program is compiled into a Java servlet before execution. Once it is compiled into a servlet, it's life cycle will be same as of servlet. But, JSP has it's own API for the lifecycle.
Receives HTTP requests from users and provides HTTP responsesEasier to write than servlets as it is similar to HTML.
We can not build any custom tagsOne of the key advantage is we can build custom tags using JSP API (there is a separate package available for writing the custom tags) which can be available as the re-usable components with lot of flexibility
Servlet has the life cycle methods init(), service() and destroy()JSP has the life cycle methods of jspInit(), _jspService() and jspDestroy()
Written in Java, with a few additional APIs specific to this kind of processing. Since it is written in Java, it follows all the Object Oriented programming techniques.JSPs can make use of the Javabeans inside the web pages
In MVC architecture Servlet acts as controller.In MVC architecture JSP acts as view.
Servlet advantages include:
1. Performance : get loaded upon first request and remains in memory idenfinately.
2. Simplicity : Run inside controlled server environment. No specific client software is needed:web broser is enough
3. Session Management : overcomes HTTP's stateless nature
4. Java Technology : network access,Database connectivity, j2ee integration
JSP Provides an extensive infrastructure for:
1. Tracking sessions.
2. Managing cookies.
3. Reading and sending HTML headers.
4. Parsing and decoding HTML form data.
5. JSP is Efficient: Every request for a JSP is handled by a simple Java thread
6. JSP is Scalable: Easy integration with other backend services
7. Seperation of roles: Developers, Content Authors/Graphic Designers/Web Masters

If you have any questions or personal guidance on learning the Servlet and JSP programming, please send a mail to krishnas at javabeat.net or write it in the comments section.

Previous Tutorial : ServletConfig in Servlet  || Next Tutorial : MVC Architecture Using JSP and Servlet

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!