• 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

init() vs constructor

 
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the servlet class need to have an init() method? Can't all the initializations be done in the constructor?

Thanks in advance.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

If you want to define a constructor in a servlet, you can only define a no-parameter constructor but in init() mathod we take SrevletConfig as
a papameter so in constructor we can not pass SrevletConfig
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any constructor can be declared in a servlet - even a one which accepts ServletConfig object. The below code builds and works fine.


The container passes on the config object only to the init() method and not to the constructor - we need not specify the constructor when we write the servlet class - default constructor comes into play (instantiation of servlet).
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edmund,

I posted same question a few days ago, not sure how to provide reference of tht, here are two replies i got
---------------------------------------------
Servlet's init method is invoked after container instantiates 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..
---------------------------------------------------
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)
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/169800/java-Web-Component-SCWCD/certification/Servlet-init-method
 
Edmund Yong
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The container does some initialization after the constructor call and before calling init(). Why can't the container finish the initialization before calling the constructor (before instantiating the servlet)? In this way, we won't even need the two init() methods at all.
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me see if this explains it

1) Container initializes the servlet
2) Container reads DD and creates the config(ServletConfig) object - this config object lies within the scope of ServletContext (i.e a servlet's web application view). This mandates the creation of servlet prior to setting the config object in the servletContext scope
3) Container passes on the config object to the init(config) method.


Why can't the container finish the initialization before calling the constructor (before instantiating the servlet)?
The container places the config object within a servlet's web view - the initialized servlets web view.

 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic