Sireesh Vattikuti
Sireesh Vattikuti

Reputation: 1190

How many ways a servlet can be destroyed?

Can the servlet be destroyed only using the destroy() method called by the contains or is there any other way

Upvotes: 0

Views: 298

Answers (2)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

The servlet is not destroyed when you call destroy method. This method is meant to clear any resources used by the servlet (initialized in init method, hopefully). The Servlet container is in charge of the lifecycle of the object reference and will call the destroy method before freeing the object reference.

Calling the destroy method in the object reference of your servlet won't assure the servlet will be removed from the memory.

The Servlet API doesn't provide a method to unregister servlets. This is the work of the servlet container. The javadoc of Servlet states it:

This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:

  1. The servlet is constructed, then initialized with the init method.
  2. Any calls from clients to the service method are handled.
  3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

Which means, not even calling the destroy method assures the servlet will be destroyed.

Upvotes: 2

sparsh610
sparsh610

Reputation: 1601

Either by calling destroy method or by shutting down the server.

Upvotes: 0

Related Questions