Servlet Container calls three methods during the different stages of a Servlet. These methods are part of Servlet interface,
which is used to develop Servlets. The Servlet interface is part of javax.servlet package.
Let's see and understand each of these methods one by one -
init(ServletConfig)
service(ServletRequest, ServletResponse)
destroy()
init(ServletConfig)
This method is executed only once in the lifetime of a Servlet instance.
When the init() method is called by the Servlet Container?
Servlet Container calls the init() method on the Servlet object after the successful
loading and instantiating of the required Servlet.
What the successful execution of init() method means?
The successful execution of init() method by the Servlet Container signifies the Servlet
object is successfully initialized and it is being put into the service.
What if an exception is thrown during the execution of init() method?
The init() method could return an exception of type ServletException which signals that
the initialization of the Servlet object has failed.
Syntax of init() method and how it is implemented?
init() method is passed as an object of type ServletConfig, which allows us to access or
manipulate Servlet's configurations or its initialized parameters.
public class TestServlet implement Servlet
{
public void init(ServletConfig sc) throws ServletException
{
}
}
service(ServletRequest, ServletResponse)
The service() method is called as many times as an Servlet instance has to handle and
respond to an user request.
When the service() method is called by the Servlet Container?
Servlet Container calls the init() method on the Servlet object after the successful
initialization of the required Servlet object i.e. after init(ServletConfig) method
returns an initialized object of the Servlet.
Why service() method is called by the Servlet Container?
Servlet Container calls the service(ServletRequest, ServletResponse) method on a
Servlet's instance to let the Servlet handle http or non-http request made by the client
and to allow it send back an appropriate response.
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 and pass
these request and response objects while calling service() method, 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. These request and
response objects are passed while calling service() method.
What if an exception is thrown during the execution of service() method?
The service() method can throw either ServletException or IOException during its
execution, which means that the Servlet can't be put to the service to handle user
requests and has resulted in an error.
Advertisement
Syntax of service() method and how it is implemented
public class TestServlet implement Servlet
{
public void service(ServletRequest, ServletResponse) throws ServletException, IOException
{
}
}
destroy()
The Servlet Container calls the destroy() method just once in the lifetime of a Servlet
instance.
When and why the destroy() method is called by the Servlet Container?
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 after the destoy() method is called?
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.
Why implementing a destroy() method is useful?
We can implement destoy() method in our Servlet class to perform some finalization steps
before the Servlet instance is destroyed by the Servlet Container.