• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why using init() method

 
Vasantha Prabha
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we are using init(),init(ServletConfig config) methods to initialize instead of declaring constructor?

Regards,
Sangeetha
 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be because in constructor u dont get ServletConfig as a parameter.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are intended to run in a "servlet container" just like applets run in an "applet container".
A servlet container manages creation of a servlet instance and calls init with a ServletConfig so that you can initialize your code AFTER the container has handled its responsibilities.
Bill
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always use the init() method when i have to connect to the database for example...because it wastes time to connect it..Then init() is usefull to do some things before.
 
Basavaraju Banakar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
init method is called once and before any of the request to the servlet are serviced. all that is required by the servlet that are common across requests are initialized here..like creation og DB connection....
 
Vasantha Prabha
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I accept we need init(ServletConfig config) method to initialize
initparameter.why do we need this init() method.why can't we can use
init(servletConfig config) itself for all initialization things
Regards,
Sangeetha
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is to allow your code to be simpler if you need less features of the servlet API.
In some cases, and application will need to access the servlet config during init. In this case it makes sense to use the more complex init(ServletConfig config). Remember, though, that you must make sure to call "super.init(config) before the rest of your init processing - this is often missed, and can lead to all sorts of confusion later.
If your application does not need to access the ServletConfig during init, it makes much more sense to just implement the simpler init() method. By the time the comtainer calls this, it has already done the equivalent of super.init(config) for you, so you don't need to worry about forgetting it.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Engin Okucu:
I always use the init() method when i have to connect to the database for example...because it wastes time to connect it..Then init() is usefull to do some things before.

I hope you don't mean you're creating Connections in the init() and keeping those alife as globals?
That's extremely poor practice and will work only with extremely low load.
 
John Dunn
slicker
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want you can have your servlet, "xyz" extend a "baseXyz" servlet. The baseXyz can contain the init() and hide alot of that "one-time" set up code. Once you get those init parts working well you can kind of forget about the base class and concentrate on doGet(), etc.
 
Vasantha Prabha
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your application does not need to access the ServletConfig during init, it makes much more sense to just implement the simpler init() method. By the time the comtainer calls this, it has already done the equivalent of super.init(config) for you, so you don't need to worry about forgetting it.
**************************************************************************
Heyyy If the init() method internally call the super.init(config) then
why we need this init(SerlvetConfig config) method???.We can directly get the getInitParameter() in all the method?
Regards,
Sangeetha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic