views:

794

answers:

4

Can I declare parameterized constructor inside servlet which is only constructor ?

If no then why ?

A: 

Since servlets are instantiated by the container they need a no-argument constructor.

Additionally the container may reuse servlets and will not call the constructor on reuse.

Steve B.
+1  A: 

You'll need to initialize the variables through <servlet-param> or use a Framework like Spring which can allow you to Proxy the Servlet to a different class that acts just like any other bean.

Gandalf
+6  A: 
erickson
But why it is expecting default constructor ? We can invoke parameterized constructor also using reflection.
Silent Warrior
Then how will it know, what values to pass in the constructor?
artemb
Have you had a look at what Servlet 3.0 provides yet?
Thorbjørn Ravn Andersen
A: 

See my answer to a similar question.

gawi