A Java Servlet is basically used to extend the functionality of a server. A Servlet is a Java programming language class. It is used commonly to expand the functionality of an application hosted on web server.

A Java Servlet adheres to the Java Servlet API in Java EE. Java Servlet API is a protocol that defines how a server request is handled by a Java class. A Servlet is majorly used over the HTTP protocol; however it is also equipped to communicate over any client-server protocol.

A programmer can create dynamic client-server web application using Servlet in the Java language. The final output of a Servlet is mostly HTML; however it can be implemented to output XML too. Since HTTP is a state-less protocol, Servlet helps maintain states between multiple HTTP requests using different methods like URL Re-writing, Sessions, Cookies, etc.

A Java Servlet class must inherit the javax.servlet parent class, which defines the appropriate execution lifecycle of a Servlet. The current version of the Java Servlet specification is 3.0, released in December 2009.

Java Servlet Lifecycle

Every Java Servlet undergoes a well-defined lifecycle of execution before generating the final output. The 3 major stages of this lifecycle are:

  1. Init
  2. Service
  3. Destroy

Lifecycle of a Java Servlet

During the Init stage, a Servlet instance in initialized by the web container using the init() method. Once initialized, a Servlet can cater to the client’s request using the service() method. Each request is then executed in a separate thread. This service() method is smart enough to detect what kind of request is made, and the same is delegated to the appropriate method. It is the duty of the Java developer to rightly implement the required methods. After completion of the service execution, the web container calls the destroy() method to kill and free resources of a Servlet. Garbage collection also happens at this stage. Like init(), destroy() is also called only once in the entire lifecycle of a Servlet.

Therefore, the 3 methods – init(), service() and destoy() – play a very important role in execution of a Servlet.

Few examples of scenarios where Java Servlet are used:

  1. To maintain and capture information across multiple request. Like collecting and maintaining data of a customer’s shopping cart.
  2. Interact with the database to capture and store data originating from HTML forms, XML files, or external interfaces.

Advantages of Java Servlet

  1. Portability is a major advantage. A Servlet can be deployed across platforms using the Java Virtual Machine.
  2. Java Servlet helps make a dynamic web application, using the vast set of Java APIs.
  3. Because Java doesn’t support the concept of pointers, building a Java Servlet gives more security than other counterpart technologies.
  4. A Java Servlet can handle other operations on the server too – like logging, scheduling, etc.
  5. Using the concept of Threading, a Java Servlet executes in a much lesser time frame.