Servlet Execution Flow
Servlet Life cycle
In the figure 1 we saw the activities a container needed to perform when a Servlet gets called. Now we will see what activities a container has to do in order to make a servlet available. And that is the mechanism of a Servlet life cycle.
1. Loading.
Container first creates an instance of a servlet by using Class.forName(ServletName).newInstance()
That is the reason why we do not have any constructor for a servlet. The container does the job for us. This process is also called the instantiation. The next question which will come to your mind is how does the container comes to know which servlets to load and where to find them. The answer is simple. For each web apps there is a deployment descriptor file associated with the web apps,and that is called web.xml. We will study details about web.xml shortly,but just keep in mind that web.xml has got an entry for the servlets associated with that web application. When the container starts,one of the activities done is to parse the web.xml and read the class names of the servlet and load them one by one. The servlet is loaded.
2. Initialising
The container initialises a servlet by calling init(ServletConfig) of the Servlet. The ServletConfig object contains all the initialising parameter read from web.xml,for this Servlet. The init() method is called only once by the container for a servlet instance. This makes sense because there is no point in initialising a servlet instance again and again. The servlet is now initialised.
3. Servicing
Servlet is now in a ready state for servicing a client request. Any request received from the client is despatched to the service() method of the servlet instance. We have already seen in Figure 1 how a request goes to the service method. A servlet instance is now said to be in the servicing stage. When a service() method is called a Servlet is in Servicing stage.
4. Destroy
A servlet container can destroy a servlet instance by calling the destroy() method . We should write cleanup scripts in here,like closing data base connections,file handlers etc. Container will call this method only once when once all threads within the servlet's service method have exited
5. Unloading
Once the servlet instance is destroyed the container may garbage collect it. The servlet is said to be unloaded.
Servlet Life cycle
In the figure 1 we saw the activities a container needed to perform when a Servlet gets called. Now we will see what activities a container has to do in order to make a servlet available. And that is the mechanism of a Servlet life cycle.
1. Loading.
Container first creates an instance of a servlet by using Class.forName(ServletName).newInstance()
That is the reason why we do not have any constructor for a servlet. The container does the job for us. This process is also called the instantiation. The next question which will come to your mind is how does the container comes to know which servlets to load and where to find them. The answer is simple. For each web apps there is a deployment descriptor file associated with the web apps,and that is called web.xml. We will study details about web.xml shortly,but just keep in mind that web.xml has got an entry for the servlets associated with that web application. When the container starts,one of the activities done is to parse the web.xml and read the class names of the servlet and load them one by one. The servlet is loaded.
2. Initialising
The container initialises a servlet by calling init(ServletConfig) of the Servlet. The ServletConfig object contains all the initialising parameter read from web.xml,for this Servlet. The init() method is called only once by the container for a servlet instance. This makes sense because there is no point in initialising a servlet instance again and again. The servlet is now initialised.
3. Servicing
Servlet is now in a ready state for servicing a client request. Any request received from the client is despatched to the service() method of the servlet instance. We have already seen in Figure 1 how a request goes to the service method. A servlet instance is now said to be in the servicing stage. When a service() method is called a Servlet is in Servicing stage.
4. Destroy
A servlet container can destroy a servlet instance by calling the destroy() method . We should write cleanup scripts in here,like closing data base connections,file handlers etc. Container will call this method only once when once all threads within the servlet's service method have exited
5. Unloading
Once the servlet instance is destroyed the container may garbage collect it. The servlet is said to be unloaded.

service method have exitedIn the figure 1 we saw the activities a container needed to perform when a Servlet gets called. Now we will see what activities a container has to do in order to make a servlet available. And that is the mechanism of a Servlet life cycle.
1. Loading.
Container first creates an instance of a servlet by using Class.forName(ServletName).newInstance()
That is the reason why we do not have any constructor for a servlet. The container does the job for us. This process is also called the instantiation. The next question which will come to your mind is how does the container comes to know which servlets to load and where to find them. The answer is simple. For each web apps there is a deployment descriptor file associated with the web apps,and that is called web.xml. We will study details about web.xml shortly,but just keep in mind that web.xml has got an entry for the servlets associated with that web application. When the container starts,one of the activities done is to parse the web.xml and read the class names of the servlet and load them one by one. The servlet is loaded.
2. Initialising
The container initialises a servlet by calling init(ServletConfig) of the Servlet. The ServletConfig object contains all the initialising parameter read from web.xml,for this Servlet. The init() method is called only once by the container for a servlet instance. This makes sense because there is no point in initialising a servlet instance again and again. The servlet is now initialised.
3. Servicing
Servlet is now in a ready state for servicing a client request. Any request received from the client is despatched to the service() method of the servlet instance. We have already seen in Figure 1 how a request goes to the service method. A servlet instance is now said to be in the servicing stage. When a service() method is called a Servlet is in Servicing stage.
4. Destroy
A servlet container can destroy a servlet instance by calling the destroy() method . We should write cleanup scripts in here,like closing data base connections,file handlers etc. Container will call this method only once when once all threads within the servlet's service method have exited
5. Unloading
Once the servlet instance is destroyed the container may garbage collect it. The servlet is said to be unloaded.

One important thing you must keep in mind is that a container creates one and only one instance of a servlet,unless the servlet implements SingleThreadModel. Multiple concurrent request are handled by container through a pool of threads.
In the next part we will study more about the deployment descriptor (web.xml),ServletConfig,ServletContext,and more about ServletRequest and ServletResponce.
Jsp Discussion
- - Two forms in one JSP
- - PASS VARIABLES BETWEEN 2
- - Table data
- - Unable to laod the image
- - The Ultimate Web UI Frame





