Header Ads

  • Breaking Now

    What is a servlet? Explain its lifecyle.

    The Servlets are server side java programs, which are used to generate dynamic web content for a web clients. They reside inside a servlet container on a web server or an application server. The servlet container provides them a runtime environment.

    If an instance of servlet is non existent then web container loads the servlet class and creates an instance of the servlet.Once the servlet instantiates, web container calls init() method on it to initialize the servlet.This process of initialization can be customized to allow servlet to read persistent configuration data,initialize resources like database connections etc. by overriding init() method of Servlet interface.If initialization of a servelet fails it throws UnavailableException.

    Once initialization is done, web container invokes the service method, passing a request and response object depending upon incoming request.

    If the container needs to remove the servlet(e.g.when web container is shutting down), it finalizes the servlet by calling the servlet's destroy method.

    The javax.servlet.Servlet interface defines the three life-cycle method:-

    public void init(ServletConfig config) throws ServletException

    public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException

    public void destroy()

    Post Top Ad

    Post Bottom Ad