aspose file tools*
The moose likes Servlets and the fly likes Why cann't we initialize servlets in constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why cann Watch "Why cann New topic
Author

Why cann't we initialize servlets in constructor?

chaminda darshana kiriwendala
Greenhorn

Joined: Jun 28, 2005
Posts: 2
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

Joined: Aug 03, 2007
Posts: 20
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

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: 42292
    
  64
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.


Ping & DNS - my free Android networking tools app
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218

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

Joined: Aug 29, 2007
Posts: 212

Servlet is instantiated by container, How container may know that what arguments needs to be passed to a servlet?


[Servlet tutorial] [Servlet 3.0 Cook Book]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Varun Narang
Ranch Hand

Joined: Nov 19, 2004
Posts: 30
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
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;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why cann't we initialize servlets in constructor?