Hi, I have written a simple servlet file, in which I have overriden all methods like doGet, service, init. According to servlet documentation, init() is the method which suppose to be get exceuted. But in my case..it didn't. Instead, everytime, service() method gets exceuted. Could anyone please let me know why such thing is happening? Actually I have to write connection method in init() method..but bcos of this problem..I am not able to do that... Please advice... Thanks
Joined: Apr 11, 2001
The init() method, should and does, get executed the first time your servlet is created. Not everytime it is accessed. Shutdown and restart your server.. Put some out.write in the init() and you will see that happening the <b>first</b> time you access the servlet. HTH
Hmm.... I should mention I've duplicated this result using Resin2.0.0
I'm not sure where to put "plain old servlets" in Tomcat, like they are easy to do in Resin, so I'll check Tomcat next. You are right... your code only produces an 'in service' I even tried System.out.println's and flushing the out. Nothing would make anything output for the init method.
As was stated previuorly, the init method gets exceuted the first time the servlet is accessed, ie when the Server loads the servlet. It has nothing to do with a request or response, so it can't return anything to the client, it's purpose is to set up data specific to this servlet. The original code:
overloaded the init method but since it doesn't match the method that gets called it did nothing. The version that Mak suggested:
was the version that would be called, except that the variable 'out' was not defined and so this version wouldn't compile. What you need to try is something like this:
Normally I wouldn't recommend using System.out in a servlet but this is fine for testing... Does this help? Dave.