life cycle of a servlet

The life cycle of a servlet can be categorized into four parts:
  1. Loading and Inatantiation
  2. Initialization
  3. Servicing the Request
  4. Destroying the Servlet

1. Loading and Inatantiation: The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the attribute of web.xml file. If the attribute has a positive value then the servlet is load with loading of the container otherwise it load when the first request comes for service. After loading of the servlet, the container creates the instances of the servlet.

2. Initialization: After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to the init() method. The init() must be called by the servlet container before the servlet can service any request. The initialization parameters persist untill the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet.

The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet.

3. Servicing the Request: After successfully completing the initialization process, the servlet will be available for service. Servlet creates seperate threads for each request. The sevlet container calls the service() method for servicing any request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object.

4. Destroying the Servlet: If the servlet is no longer needed for servicing any request, the servlet container calls the destroy() method . Like the init() method this method is also called only once throughout the life cycle of the servlet. Calling the destroy() method indicates to the servlet container not to sent the any request for service and the servlet releases all the resources associated with it. Java Virtual Machine claims for the memory associated with the resources for garbage collection.

Always Remember
init() and destroy() methods will be called only once during the life time of your Servlet while service() and it's broken down methods ( doGet(), doPost() etc ) will be called as many times as requests are received for them by the Servlet Container.

We have now finished learning about Servlet life cycle and we now know about init() , service() and destroy() methods. We can now move on to learn more about HttpServlet class which almost all of your Servlets will extend.

Example:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InitDestroyCounter extends HttpServlet
{
int count;
public void init() throws ServletException
{
// Try to load the initial count from our saved persistent state
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try
{
fileReader = new FileReader("InitDestroyCounter.initial");
bufferedReader = new BufferedReader(fileReader);
String initial = bufferedReader.readLine();
count = Integer.parseInt(initial);
return;
}
catch (FileNotFoundException ignored) { } // no saved state
catch (IOException ignored) { } // problem during read
catch (NumberFormatException ignored) { } // corrupt saved state
finally {
// Make sure to close the file
try {
if (bufferedReader != null)
{
bufferedReader.close();
}
} catch (IOException ignored) { }
}
// No luck with the saved state, check for an init parameter
String initial = getInitParameter("initial");
try
{
count = Integer.parseInt(initial);
return;
}
catch (NumberFormatException ignored)
{
} // null or non-integer value
// Default to an initial count of "0"
count = 0;
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//...
}
public void destroy()
{
4
super.destroy(); // entirely optional
saveState();
}
public void saveState()
{
// Try to save the accumulated count
FileWriter fileWriter = null;
PrintWriter printWriter = null;
try
{
fileWriter = new FileWriter("InitDestroyCounter.initial");
printWriter = new PrintWriter(fileWriter);
printWriter.println(count);
return;
}
catch (IOException e)
{ // problem during write
// Log the exception.
}
finally {
// Make sure to close the file
if (printWriter != null)
{
printWriter.close();
}
}
}
}
See Also Methods of HttpServlet class

No comments:

Google Search