• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explicit call to destroy() ....possible???

 
Sakthi Kani
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Can we call destroy() method in servlets from service method ?



public class Test extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("Hello");
destroy();
out.println("World");
}

};


output:
Hello
World

How 'World' is getting printed after the detroy() method called?
Please explain me.

Thanks in advance.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find it in servletcollections link found in my signature. check out archives section.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How 'World' is getting printed after the detroy() method called?
Please explain me.


All of a servlet's service methods should be complete when a servlet is removed. The server tries to ensure this by calling the destroy method only after all service requests have returned or after a server-specific grace period, whichever comes first. If your servlet has operations that take a long time to run (that is, operations that may run longer than the server's grace period), the operations could still be running when destroy is called.
 
Sakthi Kani
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am clear now.
Thanks your reply.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

All of a servlet's service methods should be complete when a servlet is removed. The server tries to ensure this by calling the destroy method only after all service requests have returned or after a server-specific grace period, whichever comes first. If your servlet has operations that take a long time to run (that is, operations that may run longer than the server's grace period), the operations could still be running when destroy is called.


I think this is true, when the container is going to destroy the servlet.

But in the above example, we are calling the destroy() method explcitly. This will not remove/or destroy the servlet instance at all. This is the normal call like any other method called by the user on the servlet. So, it simply run the code in destroy() method and resume the execution of the service method. Running of destroy() method does not means that the servlet is no longer available. In the servlet life-cycle the servlet call the destroy() method to clean any resources before removeing the servlet.

Think it help you.

Thanks
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Narendra Dhande.
The destroy() is a callback method that will be called when servlet instance was destroyed by container.
If you call destroy() it's just a normal method call.
 
Sakthi Kani
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.
got the exact point now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic