Why can not we call servlet constructor instead of init method to initialize the config parameters?
2 Answers
By specification:
The lifecycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
If an instance of the servlet does not exist, the web container
Loads the servlet class.
1.1. Creates an instance of the servlet class.
1.2. Initializes the servlet instance by calling the init method. Initialization is covered in Creating and Initializing a Servlet.
Invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods.

- 9,784
- 10
- 49
- 66
-
1This is how the servlet works. Not why the servlet is initialized with an init instead with a constructor with arguments. – Davide Lorenzo MARINO Feb 05 '16 at 19:09
Servlet
is an interface.
An interface can't define constructors.
This is the reason why it has been defined an init
method to call after the instantiation of the servlet. There was no other possibility to initialize the Servlet
passing a ServletConfig
.

- 26,420
- 4
- 39
- 56