What Is Servlet?

A servlet is a Java technology that is managed by a container called a servlet engine. It generates dynamic content and interacts with the client through Request and Response. Servlet extends the functionality of a web server.

What Is JSP?

JSP is a collection of technologies developed by Sun Microsystems. It is used to develop web pages by inserting Java code into the HTML pages by making special JSP tags. It can consist of either HTML or XML (combination of both is also possible) with JSP actions and commands. The full form of JSP is Java Server Pages.

KEY DIFFERENCES:

  • Servlet can accept all protocol requests, including HTTP, while JSP can only accept HTTP requests.
  • In MVC architecture, servlet works as a controller while JSP works as a view for displaying output.
  • Servlet should be used when there is more data processing involved whereas, JSP is generally used when there is less involvement of data processing.
  • Servlets run faster than JSP, on the other hand JSP runs slower than servlet as it takes time to compile the program and convert into servlets.
  • You can override the service() method in servlet but, in JSP, you can’t override the service() method.
  • In Servlet, you have to implement both business logic and presentation logic in the single file. Whereas in JSP, business logic is split from presentation logic using JavaBeans.

Why use Servlet?

  • The performance is much better.
  • Servlet is used when you do not like to create a separate process to handle each and every request of client.
  • Developers can use servlets when they have to take advantage of all the features of Java.
  • Java class libraries which are available to a servlet can communicate with applets, databases, or other software via RMI and sockets mechanisms.

Why use JSP?

  • In Java server pages JSP, the execution is much faster compared to other dynamic languages.
  • It is much better than Common Gateway Interface (CGI).
  • Java server pages (JSP)are always compiled before its processed by the server as it reduces the effort of the server to create process.
  • Java server pages are built over Servlets API. Hence, it has access to all Java APIs, JNDI, JDBC EJB, and other components of java.
  • JSP is an important part of Java EE (Enterprise Edition), which is a platform for enterprise-level applications.

Servlet Life Cycle

Servlet Life Cycle

JSP Life Cycle

JSP Life Cycle
  1. Compilation of JSP page(Compilation of JSP page into _jsp.java)
  2. Classloading (_jsp.java is converted to class file _jsp.class)
  3. Instantiation(Object of generated servlet is created)
  4. Initialisation(_jspinit() method is invoked by container)
  5. Request Processing(_jspservice() method is invoked by the container)
  6. Destroy (_jspDestroy() method invoked by the container)

Difference between a Servlet and JSP

Advantages of Servlet

  • Servlet loads only one copy of it into JVM (Java Virtual Machine).
  • It saves time to respond to the first request which increases response time.
  • It uses standard API that is supported by numerous web servers.
  • Programming languages like platform independence and ease of development.
  • You can access the large set of APIs that are available for the Java platform.
  • The web container makes threads for handling more than one request to the servlet.
  • Easy coordination between two or more servlet to make web applications.
  • Servlet containers support many other features like sessions, resource management, persistence, security, etc.
  • Servlet usage doesn’t constrain by the web browser.

Advantages of JSP

  • It is very much convenient to modify the regular HTML.
  • We can write the servlet code into the JSP.
  • It is only intended for simple inclusions which can use form data and make connections.
  • You can easily connect with JSP with the MySQL database.
  • The performance and scalability of JSP are good because JSP allows embedding of dynamic elements in HTML pages.
  • JSP is built on Java technology hence it is platform-independent and not depending on any operating systems.
  • JSP includes the feature of multithreading of java.
  • We can also make use of exception handling of java into JSP.
  • It enables to separate presentation layer with the business logic layer in the web application.
  • It is easy for developers to show as well as process the information.

Student at Vishwakarma Institute of Technology studying MCA