File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "servlet & constructors" Watch "servlet & constructors" New topic

servlet & constructors

Rajat Kumar Patro

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.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.

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.
I agree. Here's the link:
subject: servlet & constructors
jQuery in Action, 3rd edition