There are four stages in the lifecycle of a Servlet -
Loading & Instantiation of a Servlet
Initialization of a Servlet
Request Handling by a Servlet
Destroying a Servlet
Loading and Initialization of a Servlet
When a user request is received by the Web Server/Servlet Container. It finds the
required Servlet class using the deployment descriptor file(web.xml) and tries to load it using
the regular java class loading mechanism.
After the successful loading of the Servlet class, Servlet Container instantiate the
object of this Servlet class using its no-argument constructor.
The number of instances created by Servlet Container depends on -
If this Servlet class has implementedSingleThreadModel interface then ServletContainer
may create multiple instances of this Servlet class, if needed, where each instance will
handle an individual user request.
If this Servlet class has not implementedSingleThreadModel then ServletContainer will
only create one instance of this Servlet class, which will be used to handle all the user
requests.
Initialization of a Servlet object
After the successful loading and initialization of the Servlet class by the Servlet
Container, the Servlet Container immediately calls the init(ServletConfig) method of Servlet class
to initialize the Servlet object.
The init(ServletConfig) method is called just
once and it is passed the reference of ServletConfig object, which gives an access to
Servlet's configuration data or its initialized parameters.
Only after the successful initialization of Servlet object, the Servlet Container changes
the state of Servlet to active state, making this Servlet ready to handle user requests.
Request Handling by a Servlet
After the successful initialization of the required Servlet object by the Servlet
Container, either of the two steps are performed by the Servlet Container -
The Servlet container creates the ServletRequest and ServletResponse objects to
handle the non-http request made by the client and to send back an appropriate response, or
The Servlet Container creates HttpServletRequest and HttpServletResponse objects(subtype of
ServletRequest and ServletResponse) to handle HTTP requests made by the client and to
send back the appropriate HTTP response to the client.
What is the use of creating these request and response objects?
These request objects(ServletRequest or HttpServletRequest) are required to get the
request for a resource made by the client or to get the information sent by the client along the request(could be form filled by the user)
and eventually the correct response back is sent to the client using response object
(ServletResponse or HttpServletResponse).
After successfully creating the request and response objects, the Servlet Container calls
the service(ServletRequest, ServletResponse) method of Servlet, passing the created
request and response objects.
Advertisement
Whether the Servlet class has implemented the SingleThreadModel or not?
If the Servlet class has implementedSingleThreadModel interface then Servlet Container
uses the multiple instances of this Servlet simultaneously, to handle multiple concurrent
user requests or a single instance of this Servlet class is used to handle one request at
a time.
If this Servlet class has not implementedSingleThreadModelinterface then then Servlet
Container calls the service() method on the single instance of this Servlet class to
handle either one user request at a time or by making several threads to handle multiple concurrent user
requests on the same servlet instance.
Do you know the service() may throw an exception?
The service() method may throw a ServletException or or IOException
and in this case an appropriate error response is sent back to the cilent.
Otherwise, after the successful call to the service() method, an appropriate response is sent back
by the Servlet to the Servlet Container, which ultimately sends the response back to the
client.
Destroying a Servlet
At the end, the Servlet Container may call the destroy() method on the Servlet class when an
exception is thrown during the execution of service() method, which says the Servlet is
permanently unavailable.
What happens when a Servlet decides to destroy the instance of Servlet
When a Servlet Container decides to destroy an instance of Servlet, it stops taking any
new user request for this particular Servlet. Meanwhile, the Servlet Container waits
until all the threads currently executing the service() method finish their job and
eventually the Servlet Container calls the destroy() method. Calling destroy method makes
sure there are no references to the object of this Servlet and thus making it eligible for
garbage collection.