Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init()

 
eswar kumar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
can i call a sevlet init() method from same servlet service() method ?
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you try ??
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't see why not. i call init from doPost sometimes if init failed the first time
 
Mark Webb
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If init() fails, isn't the container responsible to take that servlet "out of service"?
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
non fatally so it can give out custom error pages
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how would it receive a subsequent request if the container has taken it 'not available'?
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it hasn't. the init method returned without exception and the doGet returned a generated error message.
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why fiddle with the servlet Life Cycle!!
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic