• Post Reply Bookmark Topic Watch Topic
  • New Topic

When destroy method is called?

 
Sandip Bose
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i write one code on servlet life cycle

CODE:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletLifeCycleTest extends HttpServlet {

public ServletLifeCycleTest(){
System.out.println("ctor");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet");
}
public void service(){
System.out.println("service");
}
public void init(){
System.out.println("init");
}
public void destroy(){
System.out.println("destroy");
}

}



NOW MY QUESTION IS WHEN IT WILL PRINTS "destroy" ?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do the javadocs say about that? Can you envision a situation when that might happen?
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
destroy method would be called by the servlet container, when the servlet's services are no longer required. A couple of instances when that can happen are :

1) Container being shutdown
2) A newer/different version of servlet replacing the existing servlet.

There may be other situations when this can happen, but hope this clears some cloud.
 
Sandip Bose
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. But my confusion is that it is printing

ctor
init
doGet

because When a Servlet Container creates a new instance of the Servlet, the Servlet's constructor
will be invoked. After that the method init() will be invoked automatically by the Servlet
Container. Since the method is HTTP GET method, the doGet() will called. Why it is not printing destroy ?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of those methods is being called the second time you access a servlet?

The servlet lifecycle is described in the javadocs of the Servlet interface.
 
Sandip Bose
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to refresh the page or want to access the servlet again it will print

ctor
init
doGet
doGet
doGet

like this ......
 
Mohamed Inayath
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Dawn told stop the server and get the message printed.

 
Sandip Bose
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ..
I got the answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!