File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes overloading of init-service and destroy methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "overloading of init-service and destroy methods" Watch "overloading of init-service and destroy methods" New topic

overloading of init-service and destroy methods

devayani Devasthali
Ranch Hand

Joined: Aug 25, 2005
Posts: 40
hello All,
I am writing simple servlets .can anybody pls help in understanding the concepts?
Please correct me wherever I am wrong.
thanx in advance
actually, I have overrided init , service and destroy
now doGet will not be called automatically hence I called that explicitly inside service..
now inside service , first I have called destroy method then doget ()
logically, either I must get exception or it should not called doGet method once servlet's destroy method is called.
but practically,called both service - destroy and then doget method ...
can anybody pls clear my doubt ?
am I going wrong somewhere?
pls do reply asap.
thanx & regards
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
You should never call destroy, just like you should never call init. The servlet container calls these when a servlet is created or removed. Apart from that it sounds OK.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
If you call destroy() from service() method it becomes just an ordinary method call. It wont really unload the servlet, it will be only unloaded when the destroy method called by servlet contained and we wont have control on when it need to be unloaded.

You should never call destroy or init methods.
[ February 15, 2006: Message edited by: KJ Reddy ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

You might want to read up on the service method as well.
There is generally never a need to override it when doing normal application coding with Servlets.

In short the service method, analyses the request and calls the appropriate method (doPost, doGet, doHead, etc...) depending on type of the request. Overriding it short circuts this process.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bruno Boehr

Joined: Feb 15, 2006
Posts: 17
devayani Dev,

In addition to what the previous commenters have said, I would suggest that you describe your situation and tell us what effect you are trying to achieve by calling the destroy() method from inside service(). Chances are, a better solution might pop up

<a href="" target="_blank" rel="nofollow">Your first website in Java: easier than you think</a>
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
It looks like you need also play with java itself a bit. For some reason you have impression that when you call some methods of your class, all other classes can notice that. For example if you define a method with name flyGrog and call it, you will surprise that no flying frogs happen after.

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
I agree. Here's the link:
subject: overloading of init-service and destroy methods
It's not a secret anymore!