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

Questions by lats13

Showing Answers 1 - 11 of 11 Answers

test

  • Apr 4th, 2006
 

no we can not call destroy() method

  Was this answer useful?  Yes

Jinnovator

  • Apr 4th, 2006
 

Yes, we can call for sure. Behind the scenes we are calling one method from another for the same object. Can be done but is disastrous and can lead to strange behaviour ! Try it out if you wanna !

  Was this answer useful?  Yes

siva kumar

  • Apr 19th, 2006
 

hi , we can call the destroy() method. but it works like normal method , for example ,if u have one method in ur servlet class like show(), it acts like show() method. whenever we click on the stop link in manager page of that particular application,then only application will be stopped and it calls the destory method. At that time only the application objects permenantly destroyed.sample code: Try it out..public class TestServlet extends HttpServlet implements SingleThreadModel{ public void init(){ System.out.println("hello in init"); } public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ PrintWriter out=response.getWriter(); out.println("Selected Button Name :"+request.getParameter("button")); } public void destroy(){ System.out.println("Hello in destroy"); }}

  Was this answer useful?  Yes

Riyaz

  • Apr 21st, 2006
 

Yes,we can call destroy method in service method,

  Was this answer useful?  Yes

Ashok Kumar Dash

  • May 12th, 2006
 

Hi, we can call the destroy() inside the sevice method.But in according to me from the point when we will call destroy(),then the servlet will be destroyed.But better we should implement and check it out.

  Was this answer useful?  Yes

Shikha Dixit

  • May 17th, 2006
 

Yes, we can call destroy() method from service() method but it will be treated as any other normal method rather lifecycle method. Also destroy() method called from service() method can do any deinitialization task etc but the servlet container will not unload the servlet, that can only be done by the lifecycle destroy() method.

  Was this answer useful?  Yes

Surendra

  • Jun 27th, 2007
 

Hi,

Destroy method is a method where we can provide the actions (to free up the resources) to be performed while the servlet is getting unloaded. In general when a servlet is getting destroyed the servlet container calls this method and also does many other things. So if you want to unload a servlet you have to do all the things the servlet container does including calling the servlet method.

When you directly call the destroy method from service method the method will be successfully called as you are calling the method from same object but you are not doing all the other things the servlet does to unload the servlet. So the servlet will not be unloaded but the destroy method will be called as any other non life cycle method (like display()).

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions