What is true about the lifecycle of a servlet?
Option A. The service() method is the first method invoked by the container when a new request is received.
I think this option is correct, but the given answer says it is not correct.
Option A : the init() method is invoked first.
I assumed the servlet has been instantiated and initialized(), when a new request is received,
the container calls service() method which invokes the doGet, doPost or other do methods depending on the request's action.
I assumed the servlet has been instantiated and initialized(), when a new request is received
No, that is not always the case:
Form the Servlet specs:
The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request.
After the servlet object is instantiated, the container must initialize the servlet before it can handle requests from clients. Initialization is provided so that a servlet can read persistent configuration data, initialize costly resources (such as JDBC™ API based connections), and perform other one-time activities. The container initializes the servlet instance by calling the init method of the Servlet interface with a unique (per servlet declaration) object implementing the ServletConfig interface.
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad: