Servlet Life Cycle went through three main methods and all three main methods called by Servlet Engine or Web container.
The methods are:
1. init()
2. Service()
3. Destroy()
The methods are:
1. init()
2. Service()
3. Destroy()
1 -init() : init()
method of a servlet is called only once in its lifecycle, when a very first
request comes to servlet it makes servlet state from does not exists to
initialized state and during all other requests servlet remains already
initialized.
The container calls the init() method before the
servlet can serve any client request. This method can be overridden in our
servlet classes to write some initialization code in it. Initialization means
something like initializing your data source or other things that we want ready
before servlet starts serving the request.
2 -service() : Service
methods are the methods where actual working of servlet is defined, a service
method is called every time a request comes for a servlet. It determines which
http method should be called based on client request. There are 7 http methods
but almost in 99% of web application you will be dealing with either a doGet()
or a doPost() method.
If user request is a get request than doGet() method of servlet is called and if the request ia a post than doPost() method is called and all the business logic goes into either a doGet() or a doPost() method of servlet.
If user request is a get request than doGet() method of servlet is called and if the request ia a post than doPost() method is called and all the business logic goes into either a doGet() or a doPost() method of servlet.
3 -destroy() : This
method is called by the container when the servlet is no longer required.

No comments:
Post a Comment