• 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

Difference between init and servlet constructor

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the purpose of the init method ? why didnt they just use the Servlet constructor for initialising purpose rather than using an init .
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a frequently asked question in Javaranch. Search the Servlets forum. You can find many discussions there on this topic
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually one of my favorite questions because it is an extremely important difference, but one that a lot of people have a hard time grasping. I will give you a little bit of background on it, but then I encourage you to search the forums and read the discussions.

The constructor is called by the container simply to create a POJO (plain old java object). It is not really a servlet (or at least it cannot do any servlet like things) until it has been initialized. As a developer, you should never override the servlet's constructor.

The init method initializes the servlet, and gives it references to things like the servlet context. If you want something to happen when a servlet is initialized, override the no-arg init method and put it there.
 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the information.Now i have understood it
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As a developer, you should never override the servlet's constructor.




Whats wrong in overriding the servlet's constructor ? Anyways the super class' constructor would be called from our our constructor. Isnt it ?

Cheers,
Arvind
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic