This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Joined: Aug 03, 2007
Posts: 20
posted
0
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
Joined: Oct 26, 2006
Posts: 218
posted
0
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
Marshal
Joined: Mar 22, 2005
Posts: 35252
7
posted
0
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.
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?
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.
Your computer system is like AC, it's of no use when you open Windows ;)
jasson jasson
Greenhorn
Joined: Oct 20, 2007
Posts: 6
posted
0
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;