Header Ads

  • Breaking Now

    Explain the life-cycle methods of JSP

    JSP has following three life cycle methods:

    a) jspInit(): It is the very first method which is called by JSP container to initialize the servlet instance.All JSPs implement the javax.servlet.jsp.JspPage interface that has two methods: jspInit and jspDestroy. It is important to implement when one has to load some database driver for example.

    b)_jspService(): The JSP container calls the _jspservice() for each request and it passes the request and the response objects.The _jspService() method corresponds to the body of the JSP page. This method is defined automatically by the JSP container and should never be defined by the JSP page author. That is why _jspService() method can not be overridden.


    c) jspDestroy(): When an instance of JSP is going to be destroyed then JSP container calls this method .It is used for cleaning up Both jspInit() and jspDestroy() methods can be overridden within a JSP page.This method helps in reclaiming resources like network and database connections when a JSP instance is destroyed and JSP no longer services any client request.
    package javax.servlet.jsp;
    
    public interface JspPage extends javax.servlet.Servlet {
    
    public void jspInit();
    public void jspDestroy();
    public void _jspService(ServletRequestSubtype request,
    ServletResponseSubtype response)
    throws ServletException, IOException;
    // _jspService - depends on the specific protocol used and
    // cannot be expressed in a generic way in Java.
    }
    

    Post Top Ad

    Post Bottom Ad