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

constructors

Slaxmi Raj
Ranch Hand

Joined: Apr 20, 2012
Posts: 40
how can i pass object as parameters in parameterised constructor .?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

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.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
 
subject: constructors