• 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

Servlet init method

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java classes generally we do initalizations in a constructor,
but in case of Servlet , init() method is written , why couldn't we do the same job in a constructor of servlet.

Will it matter if we move body of init method in constructor of servlet and keep init method blank.

thanks
Amar
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet's init method is invokes after container instantiate servlet.
Servlet object made in container after constructor call, and this object requires to container to fill information like ServletConfig(i.e. init parameter info, servlet env. info, and so on..) .
if u move code from init to constructor , specialy init parameter accesing code, it will not work becaus uptill then no servletconfig set by container..
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A servlet MUST be intiailized by container before it can service requests. There are many things done by container behind the scene when it initializes a servlet but for now you can understand this one easily:

Before invoking your overridden init method container will read servlet inti parameter from deployment descriptor and store them on in a special object called ServletConfig object.

There are three ways of getting this done by container:

1. Do not override init() at all. In this case container will call hidden version of init method init(ServletConfig config) this comes from Servlet interface implementation class.

2. Override this method and call super.init() method as a very first line inside method.

3. Override init() method of Generic servlet. This method is called by init(config)

Hope I have been clear enough while explaining this!
reply
    Bookmark Topic Watch Topic
  • New Topic