e hënë, 11 qershor 2007

Explain the life cycle methods of a Servlet.

The javax.servlet.Servlet interface defines the three methods known as life-cycle
methods of servlet.

This Servlet interface has been implemented by both GenericServlet and HttpServlet.

- public void init(ServletConfig config) throws ServletException
- public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
- public void destroy()

--> init()
For the first request for the servlet, the init() method will be called to initialize the servlet parameters. For the Servlets life cycle its called only once.

--> service()
After the servlet instance is created, for sub-sequent requests a new thread is created and service() will be called.
Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet.

--> destroy()
Before the servlet instance is removed from the server memory, destroy() is called to release unwanted resources. Its called only once in servlet's life cycle.

1 koment:

Sandeep tha...

This is the most basic knowledge which beginners shall need. Thanks for sharing. More more in-depth knowledge, see Servlet life-cycle methods