Multiple choice technology web technology

Select the correct statement(s) among the following:

  1. service() method is invoked by the container once per request.

  2. service() method can be invoked only once.

  3. init() is invoked by the container only once per request.

  4. init() is invoked by the container only once per instance.

Reveal answer Fill a bubble to check yourself
A,D Correct answer
Explanation

In the servlet lifecycle, the service() method is invoked by the container for each incoming request, making it responsible for request processing. The init() method is called only once per servlet instance when it's first loaded, not for each request. This lifecycle design ensures efficient resource management - initialization happens once, while request handling can occur many times concurrently.

AI explanation

To answer this question, we need to understand the lifecycle methods in Java servlets.

Option A) A. service() method is invoked by the container once per request. - This option is correct. The service() method in a servlet is called by the container for each request made to the servlet.

Option B) B. service() method can be invoked only once. - This option is incorrect. The service() method can be invoked multiple times, once for each request made to the servlet.

Option C) C. init() is invoked by the container only once per request. - This option is incorrect. The init() method is invoked only once when the servlet is initialized, regardless of the number of requests made.

Option D) D. init() is invoked by the container only once per instance. - This option is correct. The init() method is called by the container only once when the servlet instance is created.

Therefore, the correct statements are A and D.