destroy() gets executed and the initialization process continues. It is a trick question in servlets interview.
In java servlet, destroy() is not supposed to be called by the programmer. But, if it is invoked, it gets executed. The implicit question is, will the servlet get destroyed? No, it will not. destroy() method is not supposed to and will not destroy a java servlet. Don’t get confused by the name. It should have been better, if it was named onDestroy().
The meaning of destroy() in java servlet is, the content gets executed just before when the container decides to destroy the servlet. But if you invoke the destroy() method yourself, the content just gets executed and then the respective process continues. With respective to this question, the destroy() gets executed and then the servlet initialization gets completed.
Have a look at this java servlet interview question: Servlet Life Cycle – Explain, it might help you to understand better.
Comments are closed for "What happens if you call destroy() from init() in java servlet?".
this is good info. I have a question- This is not related to JSP. This is about threads. We have Object.wait() & Thread.sleep(). Why can not we have both in a single class or vice versa? or why wait() is in a Object class?
Superb joe keep going man….
Very nice documentation. My request to u is to Add Struts documentation also.
Hi Joseph,
These are great concepts on Java and J2ee.If can share your Struts and Hibernate [ hope you have …must be]experience then it will be a great pleasure.
Thanks
Niraj
HI PARASNTH
the wait method id wait for one thred is completed and next thread is executed,her we don’t give any time.
where as sleep is , if u give some time. here one problem occur even the first thread is completed there process.next thread is execute,but before execute it will check time pried
Why it is named as java as java language?
Hi Prasanth,
Object.wait() does release the monitor ownership. But Thread.sleep() does not release the monitor ownership. which means thread.sleep() is resource intensive where object.wait() is not.
Hi,
Your way of explaination is excellent.
Thanks
@ Divya ., Answer:- Bava ani pedithe bagundadhu kaabatti., in english., it is not fare if we called it as BAVA (brother in law) .. Dont ask silly questions., useless fellow
can u explain what is resource intensive ?
e.g. thread.sleep() is resource intensive.
plz explain..
This is really helpful, Better if you post some articles on springs and Web Services also.
why wait(),notify(),notifyAll() are declared in java.lang.Object instead of java.lang.Thread
Good article Joe i liked it very much
Pls give some knowledge about Struts
Very Good Explanation
really superb blog. hats off!!!
thanks joe
Your website Designing and logo presence and exposure is too much impressive………..Article so….
It is technology.
hi..
i am abhishek yadav,
pls explain the life cycle of servlet with pic $ also a demo for it….
because you wait() on (as well as notify()) an Object (which can also be a Thread). whereas only a Thread can sleep().
Its like they could have made wait() a thread specific method, but then the developer would have to take care of its reference everywhere else so that other objects can see it waiting. Instead wait() is called on the monitor which is a part of Object class.
James Gosling came up with the name ‘Oak’ (he created this language for creating a Smart refrigerator (he hated C, so created his own!!)) while working for IBM I guess. but then the lawyers didnt agree for copyright issues as there was another language with that name. then they (Gosling’s team) came up with the name Silk, which Gosling didnt like (sounded too Gay) then they again came up with a name ‘Lyric’ which again the lawyers flushed.
So finally they just came up with a bunch of names which one or the other agreed to and the lawyers didnt object to.. Java was one of the names (as they were working in Sumatra (earlier called java) (dJakarta)).. then they fired up a random number function written in *java* itself. and the name Java was chosen..
** its not really a stupid question, of course not a scientific one, but definitely not stupid. of course they could have ended up with a name Bava had there been a Telugu in the team (no offence!) :P.
Really Good explanation …
Not only this but all topic you explain so far ….
Like your skill .
Really Good explanation …
Not only this but all topic you have explained so far.
Like your skill .
This site is really helpful.
Thanks Joe.
I have one question–
Can we call init(config) method from destroy() method?
Hi Joe,
When ill the destroy() method called by default.
@ Joe
really helpful website sir but i found a contradictory statements….
*here ur saying dat destroy wont effect but in servlet life cycle concept u said dat ” if destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected.”
can u pls explain me?
thnx in advance…
Hi Joe,
I have a question , from where and when service() method is getting called ? Is it before loading Struts-config.xml ?
grt8….
Hi,
I have developed a web application, when user enter the input and start it communicates to the servlet through Ajax call and executing and there is a option to stop the Ajax call.Ajax abort function terminates the connection, but still the servlet is thread executing.
How can i stop the servlet(thread) executing?
The reason why should i stop servlet is to avoid collapse after the second request.
How to kill a servlet manually/forcefully. I was asked this question in interview
@ Joe
really helpful website sir but i found a contradictory statements….
*here ur saying dat destroy wont effect but in servlet life cycle concept u said dat ” if destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected.”
can u pls explain me?
thnx in advance…
Plz Explain THIS???