• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why using init() method

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why we are using init(),init(ServletConfig config) methods to initialize instead of declaring constructor?

Regards,
Sangeetha
 
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
may be because in constructor u dont get ServletConfig as a parameter.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
slicker
Posts: 1108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic