When you start the web-server[like tomcat] then web-container will be started and following tasks will occur on start-up:
1.Container reads the data from web.xml[using SAX parser] and stores that data in the main-memory.[Jvm]
2.Container creates the Servlet-context object and initializes the Servlet-context object with context parameters specified in web.xml.
3.Thread pool will be created by the container.
4.All the Filters and Listeners will be initialized.
5.container checks whether any servlet is configured with <load-on-startup> tag the that servlet will be initialized.
while initializing any servlet at start-up following events occur: 1.Servlet class will be loaded.
2.Servlet instance will be created by calling the default Constructor.
3.Container creates the Servlet-config object and initializes the Servlet-config object with context parameters specified in web.xml.
4.Container calls the init() method by passing Servlet-config as a parameter.
NOTE: Anyhow servlet is initialized above said tasks will happen at each servlet start-up.
WHEN you SEND THE FIRST REQUEST TO A SERVLET FOLLWING EVENTS HAPPEN:
1.Container picks the thread from the pool and assigns the request processing.
2.Now thread will start the request processing as follows:
(A)It creates the HttpServletRequest object and initializes the HttpServletRequest object with the data coming from client(like request parameters,request headers,request cookies etc..)
(B) creates the HttpServletResponse object and initializes the HttpServletResponse object with IO Stream.
(C)calls the service() method of servlet with request and response object as parameters.
(D)Once the service method execution is done then stream associated with response object will be taken and flushed to the client over network.
3.Container returns the thread to the pool after request processing is finished.
AT THE SHUTDOWN OF SERVER FOLLOWING TASKS HAPPEN:
1.Servlet-context object will be destroyed
2.Thread pool will be destroyed.
3.Container invokes the destroy() method for all servlets.
4.Container invokes the destroy() method for all filters.
5.All listeners will be destroyed.
6.All others running objects inside the container will be destyoyed and finally container is down and unable to process any user request.