• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors

 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i pass object as parameters in parameterised constructor .?
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet needs a no-arg constructor, so the container can instantiate it.
If you were to provide a parameterized constructor only, the container wouldn't know how to call it to instantiate the servlet, so that won't work.
You could define a parameterized constructor along side an explicit no-arg constructor, but it would never be called so that is pretty much useless.
Luckily, though, after the container has created an instance of a servlet it calls a life cycle method named init() to which it passes an instance of ServletConfig.
Using this ServletConfig instance you can retrieve initialization parameters, by name. These parameters can be defined in the web.xml deployment descriptor.
It may seem like a bit of a hassle, but that would be the way to provide a servlet with the parameters it needs to initialize itself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic