Can we call service() method from destroy() method in Servlet?

11,178

Solution 1

destroy() is a lifecycle method called by the Servlet container when unloading a specific instance of the Servlet. Similarly, the container will call service() when there's a client requesting the Servlet.

Can we call service() method from destroy() method in Servlet?

Short answer: Yes, as service() is a method like any other.

Long answer: You can, but it doesn't make sense. service() requires a request and a response parameters that are usually provided by the container when the Servlet is called. If you are calling service() by yourself, how are you gonna provide those parameters? What for? Are you gonna use null on both? What good is service() for two empty parameters?

Can we call destroy() method from service() method in Servlet?

Yes, again, you can call destroy() from within the service() as it is also a method like any other. Although still strange, this could make sense sometimes, as destroy() will do whatever logic you have defined (cleanup, remove attributes, etc.).


IMPORTANT: Just bear in mind that simply calling destroy() won't unload the Servlet instance. You do not manage the lifecycle of Servlets in the program, the Servlet Container does.

Solution 2

Purpose of destroy() is to de-allocated/free all the resources used by Servlet instance. By calling destroy() container deregister servlet and its service.

Yes you can call the service(request, response) like anyohter method from destroy() but it wont be executed so its useless to call service method from destroy() as those service method never going to be called/executed, request and response will be null as those objects will not be provided by container.

public void destroy() {
      try
      { 
          doPost(null, null); // will not be executed 
          doGet(null, null); // will not be executed 
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
    }

From Java doc:

public void destroy()

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed.

After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

Share:
11,178
JDGuide
Author by

JDGuide

Hello, I am Manoj Kumar, I am passionate about java and having 7+ years of industry experience in many technology. I am an occasional blogger.

Updated on June 04, 2022

Comments

  • JDGuide
    JDGuide almost 2 years

    This is one of the interview questions I faced a few days ago:

    Is it possible to call the service() method from destroy()?

    Thanks in advance.