• 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

Why cann't we initialize servlets in constructor?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet is instantiated by container, How container may know that what arguments needs to be passed to a servlet?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic