File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes serlet init() newbie question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "serlet init() newbie question" Watch "serlet init() newbie question" New topic

serlet init() newbie question

Jay Faulk

Joined: Oct 18, 2000
Posts: 2
hi all,
i am trying to write my first servlet. i have read that the init() methid is called just once. is that once for every new session. i am connecting to the DB in the init() and want different users to connect with different Connections. so am i right in saying that each time the user visits my page, the servlet will call it's init() method?
thanks in advance.
Vladan Radovanovic
Ranch Hand

Joined: Mar 20, 2000
Posts: 216
Please correct me if I am wrong, but I think that inin() method is called only once and that is first time the servlet is loaded (invoked). I believe the service() method is the one that is invoked countless times. Anyway You need to just once create Your db connection so putting it in init() makes sense. So to summarize, I think that unless servlets method destroy() has been called, init() is called only once.
[This message has been edited by Vladan Radovanovic (edited October 18, 2000).]
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The init method is run when your servlet is first instantiated, NOT for each time a browser runs the servlet.
In other words, every time a browser runs your servlet a thread is launched. The thread runs your doGet or doPost (or whatever). The init method is run once, when the servlet object is first created.
Depending on the HTTP server your using the servlet may go away and have to be re-instantiated. In this case init is run again.
I agree. Here's the link:
subject: serlet init() newbie question
It's not a secret anymore!