Servlet Life Cycle with Diagram

Servlet:

Servlet is a small program that executes on the server side of the web application. Like Applets dynamically extend the functionality of a web server. Servlet Life Cycle can be defined as a series of steps through which a servlet goes during its life span.

Servlet Life Cycle:

There are mainly three steps in the Life Cycle of a Servlet. These are –

    1. init()
    2. service()
    3. destroy()

Servlet Life Cycle with Diagram

init():

This method is invoked when the servlet is first loaded into memory. It is possible to pass the initialization parameters to the servlet.

service():

This method is called to Process the HTTP Request. The servlet can read data that has been provided in the HTTP Request. It may also produce HTTP Response for the client.

destroy():

This method calls to relinquish any resources, such as the file handle that is allocated for the servlet. The memory is allocated and then its object can be garbage collected.

Advantages of Servlets:

(i) Servlets provide better performance in terms of processing time.

(ii) Servlets platform and systems are independent.

(iii) Servlet is faster than loading creating a new object for each request with CGI (Common Gateway Interface).

(iv) Servlet can be run on any standard web container such as Tomcat, JBoss, GlassFish Server and any operating system such as Windows, Linux, Unix, MAC etc.