my dog learned polymorphism
The moose likes Servlets and the fly likes servlet doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet doubt" Watch "servlet doubt" New topic

servlet doubt

trinadh reddy
Ranch Hand

Joined: Sep 22, 2005
Posts: 60
can i call a destroy()in doGet() explicitly?

I tried to call the init() from doGet() it works fine but when iam trying to call destroy() explicitly it is giving the nullpointer exception.

May i know the reason?
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

For your First quetion:

init(),service(),destroy() are container callback methods and you should never call those.

But Of course yes, you can call those methods. Because servlet is also a class and it follows normal Java class rules.

For Second Question:

It looks strange to me. can you post your servlet class?

Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />
trinadh reddy
Ranch Hand

Joined: Sep 22, 2005
Posts: 60

[BPSouther: Added UBB Code Tags]
[ November 16, 2006: Message edited by: Ben Souther ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

trinadh reddy,

You would do well to read through the Servlet Spec; especially the sections on the "Servlet Lifecycle". (link in my signature)

You don't instanciate servlets with the new keyword.
The container does this for you.

Also, as Prabhu venkatachalam has mentioned, the methods listed above are call back methods. In other words, they are placeholders for your code. The container calls these methods at the appropriate time, You don't call them yourself.
You can override the init and destroy methods and add any code that you want to have run at startup and shutdown.
Do not override the service method directly.
Instead override doGet or doPost.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: servlet doubt
It's not a secret anymore!