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
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.
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 cr
4. Destroying the Servlet: If the s
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:
Post a Comment