The JSP Life Cycle Events

JSPs are actually servlets, as you have already seen. Container will convert all jsp files into servlets before executing them. JSPs just provide an easy way to create components containing non-java code.

Once a JSP page is created and deployed, container will do some life cycle events, which also includes calling some of its methods similar to servlet life cycle events.


Summary of lifecycle events

  1. JSP page translation (validation, translation, compilation phases)

  2. Load Class

  3. Instantiate

  4. jspInit method is called

  5. _jspService method is called

  6. jspDestroy method is called


JSP page translation (validation, translation, compilation phases)

  1. Validation is performed before translation. Syntax checking happens and all references to tag libraries, actions and EL functions in the JSP are validated to ensure that they exist and are accessible to the page, and if there are any issues an error occurs and prevents the translation process from continuing.

    • Before validation, JSP page is converted to an XML view and this xml is view is then validated and translated.

  2. Initial JSP input page is translated into servlet source code. While doing so, it will write the print statements for each of your non java lines in the JSP (please see previous demo). This generated servlet should implement HttpJspPage or its parent JspPage based on if the environment is HTTP based or not.

    • A vendor will typically have a specialized JSP base servlet, extending and implementing all required classes or interfaces. In Tomcat, this is called org.apache.jasper.runtime.HttpJspBase. HttpJspBase extends HttpServlet and GenericServlet, and implements HttpJspPage, JspPage, Serializable, Servlet, ServletConfig (please see previous demo).

  3. Servlet source code is then compiled into a servlet class file.

  4. Outputs

    • There are two outputs within the translation phase.

      • The first is an interim output: a Java source file for a servlet.

      • The second is the compiled class file from the servlet source.

    • The class file is retained for future use, and most JSP containers may retain or give you an option whereby you can retain the servlet source for debugging purposes.

    • In Tomcat, the generated servlet Java source and compiled class during JSP translation will, by default, be kept in which directory <Tomcat-Installation-Directory>/work/Catalina/localhost/<context- directory>/org/apache/jsp.

  5. Timing

    • Even though a JSP container has discretion regarding when the translation occurs, by default (in most cases) the JSP is compiled into a servlet and then loaded the first time it is accessed.

    • This might cause a small delay for the first request, but there won’t be any delay in subsequent requests.

    • In certain application servers, you may also precompile JSPs before adding them into JARs. 

  6. Errors

    • If a page fails to translate, an HTTP request for the page should give rise to a 500 (server error) status code communicated back in the HTTP response.


Load Class

  • The servlet class generated is loaded using application’s class loader.



  • An instance of the servlet class is created.

  • For every servlet mapping declaration of the JSP in the web.xml (similar to servlets), a new instance in addition to the standard JSP file instance is created.  If I have a jsp page in the context root, and I have also registered the same JSP page in web.xml, I will have two instances of that JSP created.


jspInit method is called

  • You can override this method.


_jspService method is called

  • When requests are made, the container calls the generated servlet’s service(request, response) method, which should call the method _jspService(), passing on the request and response parameters.

  • We should not override _jspService() from our JSP page. This method represents your page source in Java code form—it’s up to the container’s page generators to implementation this method for each individual JSP.

  • However the JSP spec is flexible enough to accommodate any request /response protocol you wish to implement by overriding this method; and for that only you need to override _jspService method.


jspDestroy method is called

  • Called before the instance of the JSP servlet is nullified and garbage collected


Important points to remember

  • We cannot override servlet lifecycle methods from the JSP code.

  • The servlet class corresponding to the JSP page is created by the container and hence we cannot create a constructor for the JSP page.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)