wood burning stoves 2.0*
The moose likes Java in General and the fly likes servlet & constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "servlet & constructors" Watch "servlet & constructors" New topic
Author

servlet & constructors

Rajat Kumar Patro
Greenhorn

Joined: Jun 29, 2000
Posts: 4
Can we use constructors in servlet programming ..?
Thanks Rahul & Makar for ur kind response..
Can anybody clear me why its not possble to use constructors instead of init() method..?

[This message has been edited by Rajat Kumar Patro (edited July 09, 2000).]
Milind Kulkarni
Ranch Hand

Joined: Jun 01, 2000
Posts: 146
Nope, we can not use constructors in Servlets. Use init() instead.
Regards,
Milind
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
I agree with milind. I tested out a simple servlet with and without the constructor.
The servlet with a constructor compiled fine but would not execute at runtime.
Regds.
Rahul.
hemanshow
Greenhorn

Joined: Jun 26, 2000
Posts: 25
I think u can write a constructor in any java class. It will surely be called if u do 'new MyServlet()'. But its not called when JVM creates a Servlet for catering HTTP request. It might be done just to separate the Order creation by user and Serverlet API.
Amit Zzz Kulkarni
Ranch Hand

Joined: Jul 05, 2000
Posts: 30
Originally posted by Rajat Kumar Patro:
Can we use constructors in servlet programming ..?
Can anybody clear me why its not possble to use constructors instead of init() method..?

The constructor is related to the object creation, however init ()relates to the class creation in Applets and Servlets. Let me explain...
Who is going to instantiate the servlet ? The Web Server.
Well everybody will figure that since they had designed to have a ServletConfig object in init() and since it's obviuosly a candidate for a constructor,they have to use a constructor.
But my convoluted reasoning is that since a servlet is loaded once during startup and a servlet is memory resident between requests ,of course if it's lucky it stays forever till reloaded /Web Sever restarted ,it is more like a singleton object than a normal object so there is no need for a constructor,which by definition is used to replace a init().
Also a normal Object is coming and going, coming and going and somewhere along the line if the Garbage guy is so inclined ,picks up the trash left behind.
The Server invokes your constructor, then init(). Hence when your constructor is invoked, the ServletConfig (and through it the ServletContext) is not yet available. Although in principle you can do things in the constructor that don't rely (even indirectly) on the ServletConfig or the ServletContext, it is less error-prone to simply defer all setup to the init method. That way you know that anything that needs the ServletConfig/ServletContext will have it available.
Hope it's clear to you,the same behaviour is exihibited in Applets,AppletContext and AppletStub.
Rgds
Amit
 
 
subject: servlet & constructors
 
Similar Threads
constructors...
whats the use of a protected constructor
no constructor allowed in servlet?
choosing between init() and a constructor
can i called the constructor in servlet file?If Yes how can?If no why?