can anyone please acquaint me what is the purpose of destroy() method in servlet life cycle. I found many answers regarding this, but I'm not satisfied with these answers. In a book I read that "when the web container determines that a servlet should be removed from the service, it calls the destroy() method to allow the servlet to release any resources it is using. The common use of destroy() method is to close the database connection".
I write a simple servlet to test the servlet life cycle like this:
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class LifeCycleServlet extends HttpServlet
- {
- public void init() throws ServletException
- {
- System.out.println("In init");
- }
- public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- System.out.println("In service");
- PrintWriter pw=response.getWriter();
- pw.println("Hello from LifeCycleServlet");
- }
- public void destroy()
- {
- System.out.println("In destroy");
- }
- }
Please help me out.....