jQuery in Action, 2nd edition*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question related to Servlet Life cycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question related to Servlet Life cycle" Watch "Question related to Servlet Life cycle" New topic
Author

Question related to Servlet Life cycle

kishor dhanani
Greenhorn

Joined: Sep 22, 2005
Posts: 3
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

Joined: Aug 21, 2004
Posts: 1855
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


SCJP, SCJD, SCWCD, SCBCD
kishor dhanani
Greenhorn

Joined: Sep 22, 2005
Posts: 3
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

Joined: Jul 20, 2005
Posts: 215
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

Joined: Sep 22, 2005
Posts: 3
Thanks mate
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
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


SCWCD 1.4, SCJP 1.4
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Sep 29, 2002
Posts: 47
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

Joined: Aug 02, 2005
Posts: 27
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.
 
Consider Paul's rocket mass heater.
 
subject: Question related to Servlet Life cycle