Thursday 19 January 2017

Life cycle of Servlet

Life cycle of Servlet:
·         Servlet class loading
·         Servlet instantiation
·         Initialization (call the init method)
·         Request handling (call the service method)
·         Removal from service (call the destroy method)

Why do we need constructor in servlet if we use the init()?
Even though there is an init() method in a servlet which gets called to initialize it, a constructor is still required to instantiate the servlet. Even though you as the developer would never need to explicitly call the servlet's constructor, it is still being used by the container.

How servlet is loaded?
The servlet is loaded by:
·First request is made.
·Server starts up (auto-load).
·There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
·Administrator manually loads.

When the servlet is unloaded?
Servlet gets unloaded when:
·Server shuts down.
·Administrator manually unloads.

Different types of Servlet

·Generic servlet: GenericServlet is an abstract class that implements the Servlet interface and the ServletConfig interface.  It is protocol independent servlet. It has only init() and destroy() method of ServletConfig interface in its life cycle. It may be directly extended by the servlet, also implements the log method declared in the ServletContext interface.
·Http Servlet - HttpServlet is HTTP specific servlet. It provides an abstract class HttpServlet for the developers for extend to create there  own HTTP specific servlets

No comments:

Post a Comment