Servlet Life Cycle -





A Servlet goes through the following stages in its life time -

1 - Load class -

Web container finds the servlet class file and load it. This is from where servlet life cycle starts

2- Servlet Object created -

A default constructor instantiate the servlet object. You do not need to run this constructor, it is by default executed by your compiler.

3- init() method called -

init() method allows you to initialize your servlet before it service any client request. It runs only once in servlet lifecycle. And it must completed before service() method is called.
Syntax -
public void init(ServletConfig sc)throws ServletException
{
}  

Here sc is a ServletConfig object that contain servlet configuration and initialization parameters information.  
As Servlet is intialized it gives you -
a- javax.servlet.ServletConfig object - For each Servlet, seprate ServletConfig object is created. We can use it to pass deployment time information to Servlet.
b - javax.servlet.ServletContext object- One ServletContext is created for your application. We can use it to share data (attributes) between different Servlets.

4- service() method called -

It generate the response for client request. It is called each time when a request is made for the servlet.
It has the following syntax
public void service(ServletRequest request, ServletResponse response)throws ServletException, IOException
Where - 
request - is a ServletRequest object that contain request made by client.
response - is a ServletResponse object that contain response generated by servlet for the coming request.    

5- destroy() method called -

It is called just before Servlet destroyed. It allows us to do some work that we want to do before Servlet has destroyed (for example closing a stream).After destroy(), Servlet becomes eligible for garbage collection.