• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet constructor for initialization

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to instantiate a particular servlet, container always use default constructor thus you cant pass any argument, for instance,

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how about that

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



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64615
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why a no-argument constructor ? any specific reason for that
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic