Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() vs constructor

 
Edmund Yong
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • 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.
 
Shrawan Bhageria
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • 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).
 
amar verma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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)
 
Anand Wadhwani
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/169800/java-Web-Component-SCWCD/certification/Servlet-init-method
 
Edmund Yong
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic