This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "constructors" Watch "constructors" New topic


Slaxmi Raj
Ranch Hand

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

Joined: Mar 10, 2008
Posts: 1951

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.
I agree. Here's the link:
subject: constructors
It's not a secret anymore!