Servlet Life Cycle:
When you
deploy an application under web-app folder of tomcat home directory and start
the tomcat (i.e. deploy the application in tomcat)
1.
Web
container (tomcat) reads the Deployment Descriptor (web.xml) and creates a Servlet Context object.
2.
Container
reads the DD and search for <context- param> (if any) and creates a Key/value
pair of Strings for corresponding <param-name>
and
<param-value>.
Ex. <context-param>
<param-name>AdminEmail</pram-name>
<param-value>susheel.nitdgp@gmail.com
</param-value>
</context-param>
Will create a reference with key as ‘AdminEmail’ and value as ‘susheel.nitdgp@gmail.com’
3.
This
key/ value reference is provided to ServletContext reference.
4.
ServletContextListener
(if any) will be called.
5. Container will create ServletConfig Object.
6.
Container
reads the DD and search for any servlet initialization parameters.
Ex. <servlet>
<servlet-name>……..</servlet-name>
<servlet-class>………</servlet-class>
<init-param>
<param-name>…….</param-name>
<param-value>……..</param-value>
</init-param>
</servlet>
7.
The
key value pair of Strings will be created for init parameters (same as for
context param) and will be assigned to ServletConfig reference.
Note : no servlet is
still created till this step even servlet life cycle is not started.
8.
Container
will create request and response objects.
9.
Servlet
class will be loaded by container.
10.
Servlet
default no-arg constructor will be called and a simple java object is
created(still we don’t have servlet object).
11. First
Life Cycle method for Servlet init(ServletConfig ) will be called and servlet
object will be created and initialized.
12.
Container
will start a thread and calls the Second life cycle method and most used method
service(request, response) of the Servlet.
13. Service()
method will call doGet(request, response) or doPost(request, response) method
based on the submitted method name.
14.
Service()
method is the place where all the work is done.
15.
An
response is created by using getWriter() or getOutputStream() methods of Response
object.
16.
Response
is sent to any other servlet, jsp using Request Dispatcher or committed to
client.
17. In
later case request, response objects will be destroyed and thread will be
destroyed or sent back to connection pool managed by container.
No comments:
Post a Comment