Can Servlet Have Parameterized Constructor?

Can Servlet Have Parameterized Constructor?

4 Answers. No. Servlet instances are created by the container via reflection, and they expect to find a public, no-arg constructor (the default constructor). To configure your servlet, use servlet parameters specified in the web.

Can servlet have a constructor?

Can we define Constructor in Servlet? The short answer to this question, Yes, Servlet implementation classes can have a constructor but they should be using the init() method to initialize Servlet because of two reasons, first you cannot declare constructors on an interface in Java.

Can servlet class declare constructor with an argument?

2. Can servlet class declare constructor with ServletConfig object as an argument? Explanation: ServletConfig object is created after the constructor is called and before init() is called. So, servlet init parameters cannot be accessed in the constructor.

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.