This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serlet init() newbie question

 
Jay Faulk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Vladan
[This message has been edited by Vladan Radovanovic (edited October 18, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic