File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes servlet constructor for initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply 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: 328

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
Bartender

Joined: Jan 28, 2008
Posts: 4503

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



Not everything that counts can be counted, and not everything that can be counted counts-Albert Einstein
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 328


how about that

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



Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50693

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.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 328


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

Joined: Jan 28, 2008
Posts: 4503

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: 12513

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.


One of the most odious afflictions that Business has inflicted on the modern English language is "pro-active". Most of the time it's simply redundantly used in place of the simple old word "active". And a good deal of the rest of the time it means "You're not overworked enough yet, so go out and find more!"
 
 
subject: servlet constructor for initialization
 
Threads others viewed
Passed. :)
Passed
Passed SCJD!!!
Passed SCJP 5.0, 94%
Passed SCJD
developer file tools