understanding JSP life cycle

In this post, you will learn:

  • What is JSP life cycle?
  • How JSP life cycle is linked to the Servlet life cycle?

The JSP life cycle:

JSP life cycle is defined by JspPage and HttpJspPage interfaces. Similar to servlet, JSP life cycle has three methods: jspInit(), jspDestroy() and _jspService() .

javax.servlet.jsp package contains classes and interface of JSP API. Main classes and interfaces of the JSP API are:

JspPage: It extends Servlet interface and provides following JSP life cycle methods:

1. jspInit(): This method is provided to define initialization operations of the JSP. It has following syntax:

public void jspInit();

2. jspDestroy(): This method is provided to define clean up operations of the JSP. It has following syntax:

public void JspDestroy();

HttpJspPage: It extends JspPage interface and provides following JSP life cycle method:

3. _jspService(): This method is provided to define the request processing logic of the JSP. It has following syntax:

public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

_(underscore) as prefix of this method denotes that this method can’t be directly defined or overridden by the JSP programmer. It is auto generated in the servlet using the contents of the JSP page.

JspBase: It is an abstract class which implements HttpJspPage interface and defines all the methods except _jspService(). The Servlet which is generated for the JSP, extends it and defines _jspService() method. It is responsible for linking the servlet life cycle methods to the JSP life cycle methods. Following diagram describes how the JSP life cycle is linked to the servlet life cycle:

jsp_servlet_relation

Description of the diagram:

1.0 – First request is submitted for a JSP by a client.

1.1Servlet class is generated for the JSP and it is instantiated.

1.2 – A ServletConfig object is created for the Servlet.

1.3init() method is invoked and the reference of the ServletConfig object is passed as argument in it.

1.4init() method of the JspBase class is executed.

1.5 – From the init() method of the JspBase, jspInit() method is invoked.

1.6 & 1.6″jspInit() method of the auto generated servlet if overridden, is executed otherwise the jspInit() method of JspBase, is executed.

2.0service(request,response) method is invoked on the servlet object by the server.

2.1service() method defined by JspBase is executed.

2.2 – In the service() method of JspBase, ServletRequest & ServletResponse are type casted to HttpServletRequest & HttpServletResponse and _jspService() method is invoked.

2.3_jspService() method of the auto generated servlet is executed.


Its time to recap the main concepts:

  1. JSP life cycle is defined by JspPage and HttpJspPage interfaces.
  2. Similar to servlet, JSP life cycle has three methods: jspInit(), jspDestroy() and _jspService().
  3. JspBase class is responsible for linking the servlet life cycle methods to the JSP life cycle methods.
  4. Of the three JSP life cycle methods, only jspInit() and jspDestroy() can be overridden by the JSP programmer.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *