Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question related to Servlet Life cycle

 
kishor dhanani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What will happen if i will call destroy method from service method.
Or i will call destroy from doget or doPost method?
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kishor,

welcome to JavaRanch . The one who calls the servlet's destroy is the Container NOT you. When the Container did so, then the servlet is available (elligible) for garbage collection.

Regards,
Darya
 
kishor dhanani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But will happen if i will define my post method like this,


protected void dopost(HttpServletRequest req , HttpServletResponse res) throws ServletException , IOException{
destroy();
}


will it made instance available for garbage collection?
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

That is a very interesting question. To test it I did the following

servlet code


jsp code


I also added appropriate entries into the web.xml file.


I then called the servlet via http://localhost:8080/RanchTest/DestroyTestServlet

I got a page displaying '1' (the jsp had been called) I then left it a short while to allow the garbace collector time to clean up any objects and then called the jsp directly via http://localhost:8080/RanchTest/destroy_006640.jsp and I still get '1' The garbage collector had not called the finalize method and cleaned up the servlet.

Interestingly I can still call the servlet via http://localhost:8080/RanchTest/DestroyTestServlet and it works. I still get an html page displaying '1' (1 instance of the servlet as I would expect) and the redirect is still clearly working, so the container also still knows about the servlet

Mat
 
kishor dhanani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling destroy() method from service or doGet or doPost is just like calling another method . There will be no effect on the life cycle of the servlet untill destroy is not called by the Container . Means destroy() will work like an ordinary method till it is called by Container.



Rohan
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Life Cycle methods only controlled by container. If you use life cycle methods it just works as a method ordinary method call. Some people asks what happens if you write a constructor. When you call constructor it just bheaves like a ordinary class.
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interesting question ...

suppose we are creating a database connection in the init method and releasing it in destroy - what will happen if we ourselves call destroy in the doGet()? I do not think the container will know we called this method, and call init method again. It will believe that the servlet is fit for service. Any idea?

- Gouri
 
Alexandre Cervieri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gouri Bargi:
An interesting question ...

suppose we are creating a database connection in the init method and releasing it in destroy - what will happen if we ourselves call destroy in the doGet()? I do not think the container will know we called this method, and call init method again. It will believe that the servlet is fit for service. Any idea?

- Gouri


This is the best example to explain why you cannot call the life-cycle methods yourself. They are meant to be called by the container, and it doesn't have a way to find out whether someone else already called the destroy method, for example, or not. So, calling the destroy method in the service method in the situation you described will certainly generate a problem when your servlet will be invoked again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic