Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why constructor only is not enough for initializing the servlet ?

 
rajpal b songara
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to know why constructor only is not enough for initializing servlet ? i mean to say what ever the init() method do we can put that into a constructor.
so what is the thing we can achieve from init() method but not from constructor.

thanks.
 
Balaji Vankadaru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


We cannot write constructors with arguments in a servlet class. It will throw Exception.

If you do not intend to access servlet context and config its possible to manage your code trough constructor.
 
rajpal b songara
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Balaji For Quick Reply,

so from your answer may i consider that 'for just making servletconfig object,servlet API provide init(ServletConfig) method'.
no additional use at container level or any-other around it ?
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajpal b songara wrote:Thanks Balaji For Quick Reply,

so from your answer may i consider that 'for just making servletconfig object,servlet API provide init(ServletConfig) method'.
no additional use at container level or any-other around it ?


The container uses the init() method to actually register your class as an actual Servlet . The constructor alone is not enough to achieve this. After the init() method is executed ( and no exception has occured ), the container makes the servlet object and then registers it, after registration with the container , the servlet is then ready to be used and service any request.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif Asif wrote:The container uses the init() method to actually register your class as an actual Servlet .

Not correct. A servlet doesn't need to have an init() method at all. It is the declarations in the deployment descriptor that "register" the servlet.

After the init() method is executed ( and no exception has occured ), the container makes the servlet object

Also not correct. How could the init() method be called if the object does not already exist? Servlets are no different from other objects in Java and a method cannot be called on an instance before that instance even exists.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear wrote: A servlet doesn't need to have an init() method at all. It is the declarations in the deployment descriptor that "register" the servlet.

So you mean to say that you do not need an init() method at all to initialize a servlet ?

I quote from "Mannings Book for SCWCD"

..
The servlet is initialized after
the init() method returns
..



Now I am confused here.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif Asif wrote:So you mean to say that you do not need an init() method at all to initialize a servlet ?

Overriding one of the init() methods is completely optional.

I quote from "Mannings Book for SCWCD"

..
The servlet is initialized after
the init() method returns
..


You are confusing the word initialize with created. The instance is created long before any initialization takes place. The instance cannot be created after calling methods on it.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balaji Vankadaru wrote:We cannot write constructors with arguments in a servlet class. It will throw Exception.

Not really correct. A servlet can have any number of constructors, but if it has constructors with arguments then you must add a default constructor without arguments, because the compiler will not insert it for you. Only the default constructcor will ever be called, all others are dead weight.

If you do not intend to access servlet context and config its possible to manage your code trough constructor.

Not really correct, either. A servlet's constructor will only be called once, whereas the init and destroy methods can be called multiple times.

Simple rule of thumb: never put a constructor in a servlet.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear wrote: You are confusing the word initialize with created. The instance is created long before any initialization takes place. The instance cannot be created after calling methods on it.


Thank you Bear ! Cleared my misunderstanding !
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Simple rule of thumb: never put a constructor in a servlet.

Quoted for truth. There is no valid reason to do so.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic