This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes Calling init Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling init" Watch "Calling init" New topic
Author

Calling init

Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
Hi all,
Well "init is called only once when the servlet is requested for the first time."
What would happen if I again call the init() in service(...) or doGet(..) or any other method.
Tia
MB


Malhar Barai
SOA & Java Book
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
then it gets called each time your code is executed.
some people will tell you this is a bad idea, but under some circumstances i believe it is the best solution.


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I can't imagine any situation it would make sense for your code to call init. I can imagine plenty of situations where your code would want to do the same processing at several points, though. If that's the case, just delegate from init and your code to the same method:

[ December 09, 2003: Message edited by: Frank Carver ]

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61306
    
  66

I'm with Frank. Calling init from elsewhere in the servlet is a non-standard and unexpected thing to do. Factoring common behavior into a private method is much cleaner.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: Calling init