We need to understand task happing at following:
• Task happing at container startup.
• Task happing at request processing time.
• Task happening at container shutdown.
Consider the following case:
• I have developed one web application with the following:
o I have written two servlets with the names HelloServlet and HaiServlet.
o I have written one listener with the name HelloListener.
o I have written one filter with the name HelloFilter.
o I configured all the above components in the web.xml.
o I also configured context parameters and configuration parameters for HelloServlet.
o I specified "load-on-startup" for the HelloServlet.
• Following is the sample web.xml
<web-app>
<context-param>
<param-name>state</param-name>
<param-value>UP</param-value>
</context-param>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.jtcindia.HelloServlet</servlet-class>
<init-param>
<param-name>city</param-name>
<param-value>Noida</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello.jtc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>hai</servlet-name>
<servlet-class>com.jtcindia.HaiServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hai</servlet-name>
<url-pattern>/hai.jtc</url-pattern>
</servlet-mapping>
</web-app>
• I did the following tasks: After deploying the above-mentioned application on a server.
o Started the server.
o Send the first request to HelloServlet.
o Send the second request to HelloServlet.
o Send the first request to HaiServlet.
o Send the second request to HaiServlet.
o Shutdown the server.
(Case-1) Started the Server.
1. Container reads information from web.xml and stores it in the main memory using the SAX parser. If any problem happened while reading the web.xml container, it will not be ready to receive the request from the server.
2. Container creates a ServletContextObject and initialises the context object with the context parameters specified in web.xml
3. Containers creates the thread pool.
4. All the listeners configured in the web.xml will be initialised (it is a Java class loading, creating instances,initialization and installation.)
5. All the filters configured in web.xml will be initialised.
6. Container checks whether any servlet is configured with the load-on-startup> tag. If any one or more servletsfound with the load-on-startup> tag, then those servlets will be initialised by the container at container startup with priority by doing the following tasks:
a. Container loads the Servlet class into main memory.
b. Container creates an instance of a servlet by invoking the default constructor.
c. Container creates a servlet config object and initialises the config object with the config parameters specified in web.xml.
d. Container associates the servlet context object with the servlet configuration object.
e. Container invokes the init() method by passing the ServletConfig object as a parameter to initialise the servlet instance with the required resources.
(Case-2) Send the 1st request to HelloServlet.
1. Container collects the url pattern of the incoming request (/hello.jtc) and checks whether the corresponding servlet is initialised or not.
2. If the servlet is initialised, the container picks one thread from the thread pool and hand overs the remaining request processing.
3. Threads start request processing by doing the following tasks:
a. A ServletRequest object will be created and initialised with the data coming from the client with HttpRequest.
b. A HttpServlet response object will be created and initialised with the response stream.
c. The service() method will be invoked by passing the ServletRequest and ServletResponse objects as parameters.
d. Once the service() method is executed, the response stream will be flushed to the client over the network and destroys the request and response objects.
e. Once the response is delivered to the client, the thread will be returned to the pool.
interface-> ServletRequest
interface -> HttpServletRequest
interface ->HttpServletRequestImpl
ServletRequest sreq= new HttpServletRequestImpl();
service(sreq);
(Case 3) Send the 2nd request to HelloServlet.
Same as Case 2.
(Case 4) Send the 1st request to HaiServlet.
1. Container collects the url pattern of the incoming request and checks whether the corresponding servlet is initialised or not.
2. If the servlet is not initialised, the container initialises that servlet by doing the following tasks:
a. Container loads the Servlet class into main memory.
b. Container creates an instance of a servlet by invoking the default constructor.
c. Container creates a servlet config object and initialises the config object with the config parameters specified in web.xml.
d. The container associates the servlet context object with the servletconfig object.
e. Container invokes the init() method by passing a ServletConfig object as a parameter to initialise the servlet instance with the required resources.
3. If the servlet is already initialised, the container picks one thread from the thread pool and hands over the remaining request processing.
4. Threads start request processing by doing the following tasks:
a. A ServletRequest object will be created and initialised with the data coming from the client with HttpRequest.
b. A HttpServlet response object will be created and initialised with the response stream.
c. The service() method will be invoked by passing the ServletRequest and ServletResponse objects as parameters.
d. Once the service() method is executed, the response stream will be flushed to the client over the network and destroys the request and response objects.
e. Once the response is delivered to the client, the thread will be returned to the pool.
(Case-5) Send the 2nd request to HaiServlet.
Same as Case 2
(Case-6) Shutdown the Server
At container shutdown time, the following tasks will happen:
1. The thread pool will be destroyed.
2. All the servlets will be destroyed one by one. For destroying one servlet instance, container invokes the destroy () method on the servlet instance.
3. All the filters will be destroyed, one by one. While destroying one filter instance, the container invokes the destroy() method on the filter instance.
4. All the listeners will be destroyed.
5. If any session objects (private objects for use) are running in the container, those will also be destroyed.The internal container is down and is unable to process any user requests.