0

Why can not we call servlet constructor instead of init method to initialize the config parameters?

Rohit Raj
  • 801
  • 1
  • 6
  • 4

2 Answers2

0

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

  1. 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.

  2. Invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods.

nobeh
  • 9,784
  • 10
  • 49
  • 66
0

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.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56