What are the different stages in life-cycle of a servlet?

0 votes
asked Jun 22, 2016 in Java by Naveen
, , 136 views Last updated : Nov 21, 2017

2 Answers

0 votes
  • Servlet is loaded
  • servlet is instantiated
  • servlet is initialized
  • service the request
  • servlet is destroyed
answered Jun 22, 2016 by Estela
0 votes

Loading Servlet Class : A Servlet class is loaded when first request for the servlet is received by the Web Container.

Servlet instance creation :After the Servlet class is loaded, Web Container creates the instance of it. Servlet instance is created only once in the life cycle.

Call to the init() method : init() method is called by the Web Container on servlet instance to initialize the servlet.

Signature of init() method :

public void init(ServletConfig config) throws ServletException

Call to the service() method : The containers call the service() method each time the request for servlet is received. The service() method will then call the doGet() or doPost() methos based ont eh type of the HTTP request, as explained in previous lessons.

Signature of service() method :

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException.
Call to destroy() method: The Web Container call the destroy() method before removing servlet instance, giving it a chance for cleanup activity.
Creating a Servlet:
There are three different ways to create a servlet.
  • By implementing Servlet interface.
  • By extending GenericServlet class.
  • By extending HttpServlet class.
answered Feb 14 by Roshan333

Related questions

Explain servlet life cycle.

Loading Servlet Class : A Servlet class is loaded when first request for the servlet is received by the Web Container.Servlet instance creation :After the Servlet class is loaded, Web Container...

solved
java, jdbc, servlet 3 Answer 292 views asked Jun 17, 2016 in Java by Satish