wood burning stoves 2.0*
The moose likes Servlets and the fly likes init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init()" Watch "init()" New topic
Author

init()

eswar kumar
Ranch Hand

Joined: Oct 20, 2002
Posts: 98
Hi
can i call a sevlet init() method from same servlet service() method ?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
did you try ??
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
i don't see why not. i call init from doPost sometimes if init failed the first time


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Mark Webb
Greenhorn

Joined: Aug 31, 2002
Posts: 10
I am not sure what you are trying to do, but I think the logic could be changed to support the code in the init from executing multiple times, or making sure the code is executed at all.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12681
    
    5
Your code should never call init() - that is the responsibility of the servlet engine. The API says the init method will be called once before any request is processed.
If you want init to do something that may also have to be done multiple times during the lifetime of the servlet, put it in a separate method.
An example might be reading configuration files.
Bill


Java Resources at www.wbrogden.com
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
Yup,
Never call init() anywhere, thats always the responsibility of the container to call when the servlet is loaded, its just as destroy() method, its never has to be called.
But, as some others pointed out, why would you like to call init() elsewhere !!??
MB


Malhar Barai
SOA & Java Book
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
if the procedures to load the servlet in init failed the first time and a subsequent doGet is received then you give init another try. this lets you fix problems in the servlet loading without having to reload tomcat or the context
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If init() fails, isn't the container responsible to take that servlet "out of service"?
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
non fatally so it can give out custom error pages
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

so how would it receive a subsequent request if the container has taken it 'not available'?
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
it hasn't. the init method returned without exception and the doGet returned a generated error message.
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
Still if the container has found problems with init(), or hasn't recognized/accessed init(), how would it recognize/access the servlet request...
MB
Trijoy Saikia
Greenhorn

Joined: Nov 20, 2003
Posts: 3
Why fiddle with the servlet Life Cycle!!
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
Originally posted by Malhar Barai:
Still if the container has found problems with init(), or hasn't recognized/accessed init(), how would it recognize/access the servlet request...
MB

it only works when the servlet is at least setup to be called right obviously
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: init()
 
Similar Threads
loading of static attributtes
Final value in a Servlet
init missing in servlet
Init and Constructor
Constructor inheritence