aspose file tools*
The moose likes Servlets and the fly likes servlet constructor for initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet constructor for initialization " Watch "servlet constructor for initialization " New topic
Author

servlet constructor for initialization

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ranchers ,

i have read following reason why servlet cannot have a constructor.

reason: ServletConfig object could not be passed in constructor but why ?

my question : can't it be passed like any other parameter in a constructor ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

to instantiate a particular servlet, container always use default constructor thus you cant pass any argument, for instance,

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


how about that

Class.forName("com.TestServlet").newInstance(ServletConfig cfg);



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

As stated, the servlets need to be instantiated using the nullary constructor, and so you cannot define a non-nullary constructor and expect it to be used. Any initialization that relies upon any context information must wait until the init() life-cycle method is called. You could, I suppose, perform simple initialization in a nullary constructor, but to do so is counter to convention and therefore a poor practice.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


why a no-argument constructor ? any specific reason for that
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

naveen yadav wrote:
why a no-argument constructor ? any specific reason for that

otherwise newInstance() method in my previous post throw java.lang.InstantiationException
Chandraprakash Sarathe
Greenhorn

Joined: Jan 21, 2012
Posts: 16
Very well explained the reason at http://oreilly.com/catalog/jservlet/chapter/ch03.html

The init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests. Why not use a constructor instead? Well, in JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments. So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface. Also, Java doesn't allow interfaces to declare constructors. This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter. It has to declare another method, like init(). It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException.


Chandraprakash Sarathe
http://javaved.blogspot.com
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Bottom line. Servlets follow the JavaBean architectural paradigm. One of the core principles of JavaBeans is that they should provide a no-argument constructor method so that a simple generic brain-dead BeanClass.newInstance() can be used to construct the bean without regard to environment or context. Environmental/contextual considerations should be managed post-construction. In the case of servlets, that's the init() method.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet constructor for initialization