Meaningless Drivel is fun!
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

Calling init

Malhar Barai
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.

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

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 ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

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.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Calling init
It's not a secret anymore!