Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why cann't we initialize servlets in constructor?

 
chaminda darshana kiriwendala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

Could you tell me why cann't we initialize servlets in constructor. Why do we use separate "init()" method?
[ October 17, 2007: Message edited by: Bear Bibeault ]
 
Sridhar Padala
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Servlets are called by webcontainer, so as per the servlets life cycle whenever the container creates servlet object its responsibility to call init() method to intialize variables. If you write in constructor how come container knows about it as we won't create servlet object like normal class. I hope you understand.If you have any issues,let me know.

All The Best
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by chaminda
Could you tell me why cann't we initialize servlets in constructor. Why do we use separate "init()" method?


What kind of initialization you want to do? You cant do things like getting instance of ServletConfig as it is not available before init. There is nothing stopping you from initializing the instance variables in it.


Originally posted by Sridhar
If you write in constructor how come container knows about it as we won't create servlet object like normal class


Then how is it instanciated?

Servlet objects are created using reflection by the container where the no argument contructor of the class is called
[ October 17, 2007: Message edited by: Amol Nayak ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amol Nayak:
Then how is it instanciated?

Servlet objects are created using reflection by the container where the no argument contructor of the class is called


Reflection is not used here. The no-argument constructor is called (using Class.newInstance). That's why servlets can not be instantiated through any other constructors - the container can only use the no-arg constructor.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Oricinally posted by Ulf Dittmer
Reflection is not used here. The no-argument constructor is called (using Class.newInstance). That's why servlets can not be instantiated through any other constructors - the container can only use the no-arg constructor.


That is what i meant, isnt instantiating a class using Class.newInstance a type of reflection?
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet is instantiated by container, How container may know that what arguments needs to be passed to a servlet?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't pass any arguments during instanciation.

When it it put into service the container calls the init method and passes it an instance of ServletConfig.
 
Varun Narang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Who says you can't initialize anything in Servlet'Constructor, only thing is that you won't be able to take advantage of some of beautiful features of the Servlet API, such as access to ServletConfig object. In that case you'd want to access the parameters from database/flat-file and not from web.xml initialization parameter.
Another reason for using init() method is the Servlet-Lifecycle, in which, the init method is called just before the first request hits the servlet. So, this method just saves you from doing the things too early in the whole cycle.
You can also define an overloaded constructor for the servlet, only that it'd never be called, as per the servlet specification.

Hope that makes sense to you.

Regards,
VaruNarang.
 
jasson jasson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chaminda darshana kiriwendala:
Hi friends,

Could you tell me why cann't we initialize servlets in constructor. Why do we use separate "init()" method?

[ October 17, 2007: Message edited by: Bear Bibeault ]


you can not overridding the init() method;
init(servletConfig config),service(),destroy()
are three backup method and also per the servlet life cycle ,the container has its responsibility to call init(),and servlet contrainer can init some servlet object,
if you overridding this method ,you can not get some objects ,such as servletconfig and so on;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic