Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when the destroy() methid caled

 
naresh govindaswmay
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
when servlet destroy() is called in the life cycle and teh real time.
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just before your servlet instance is garbage collected.
Like finalize() methods for objects of your java classes, you can't gaurantee when it will be exactly called.

Regards,
Khushhal
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

destroy() will be called when ever we remove the servlet instance from the service.

After completion of the destroy() method,the object is eligible for garbage collection.


Thanks

Anil Kumar
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The destroy() method is called after the service method is completed and is called before garbage collection
With regards,
Padma priya N.G.
 
naresh govindaswmay
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have attached the sample code here please check it in what situation the servlet destroy() is called..

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LifeCycle extends HttpServlet
{
public void init(ServletConfig cfg)
{
System.out.println("Inside the servlet init method");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
System.out.println("Inside the sevice method ");
HttpSession session=request.getSession();
System.out.println("the session is created");
session.invalidate();
System.out.println("the session is destroyed.......");
}
public void destroy()
{
System.out.println("inside the destroy....");
}

};

regards
naresh
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mgnaresh mgnaresh",
you've already been warned here concerning your name. Please take some time to adjust it according to the Naming Policy.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anil said:
destroy() will be called when ever we remove the servlet instance from the service.


We dont remove the servlet instance, its upto the container to decide that.

padmapriyagururajan said:
The destroy() method is called after the service method is completed and is called before garbage collection


The spec says this:

Before the servlet container calls the destroy method, it must allow any
threads that are currently running in the service method of the servlet to complete
execution, or exceed a server-defined time limit.


Naresh as the spec says, if all the threads running in service are done and if the container wishes to remove the instance then destroy is called.
You cannot force the container to do it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic