What is the lifecycle of a HttpServlet?

  • a servlet is created when the application starts (it is deployed on the servlet container) or when it is first accessed (depending on the load-on-startup setting)
  • when the servlet is instantiated, the init() method of the servlet is called
  • then the servlet (its one and only instance) handles all requests (its service() method being called by multiple threads). That’s why it is not advisable to have any synchronization in it, and you should avoid instance variables of the servlet
  • when the application is undeployed (the servlet container stops), the destroy() method is called.

Leave a Comment