This tutorial is very much important. If you say that you know the servlet same time you should know the life cycle of servlet. Servlet life cycle is most commonly asked question in servlet technology.
If any class qualifies as servlet that class must be subclass of Servlet interface directly or indirectly. The Servlet interface has three methods which play the key role in servlet life cycle. The three method of servlet life cycle are:
The life cycle of servlet start only and only when a call is giving to that servlet.
Whenever a request is going to server for a servlet to server. The server looks for a particular servlet instance. If the instance is not found, it will be created and the init() method of that servlet will be called. Calling of the init () method is one time process in life cycle of the servlet. And a thread will be allocated to process the same request.
So whenever we are giving the first request to the server for servlet. It takes the URL pattern and identify by the URL that which servlet has to call. And creating the object of that memory and calling the init() method.
If you make the second request for same servlet than server will not create a new object for that servlet and also do not call the init() method also. A new thread will created and will be allocated to handle the request.
So if ten different requests are coming at same time for same servlet than server will allocated thread to each request using one object.
After allocating thread the service () method of the servlet will be called where the main business logic will be executed and give response back to the request.
So the service method will be called for each request.
Finally whenever the servlet object become amended for much time or server is going down the destroy () method of servlet will be called.
A diagram of the servlet life cycle is given below.