Which method is called first each time a Servlet is invoked ?

  1. Start()

  2. Run()

  3. Servive()

  4. init()


Correct Option: D

AI Explanation

To answer this question, you need to understand the lifecycle of a servlet.

When a servlet is first loaded and initialized, the init() method is called. This method is called only once during the entire lifecycle of the servlet. It is used to perform any initialization tasks, such as setting up resources, loading configurations, or establishing database connections.

After the init() method is called, the servlet is ready to handle requests. Each time a request is made to the servlet, the service() method is called. The service() method is responsible for processing the request and generating the response. It determines the HTTP method (GET, POST, etc.) used in the request and calls the appropriate method (doGet(), doPost(), etc.) to handle the request.

The start() and run() methods are not part of the servlet lifecycle. They are methods of the Thread class and are used for multithreading purposes.

Therefore, the correct answer is:

D. init()

Find more quizzes: