A JSP page life cycle is defined as a process from its translation phase to the destruction phase. This lesson describes the various stages of a JSP page life cycle.
- Translation Phase
- Compilation Phase
- Initialization Phase
- Execution Phase
- Destruction(Cleanup) Phase
Let us understand these steps in detail.
Translation Phase
- When a user navigates to a page ending with a .jsp extension in their web browser, the web browser sends an HTTP request to the webserver.
- The webserver checks if a compiled version of the JSP page already exists.
- If the JSP page's compiled version does not exist, the file is sent to the JSP Servlet engine, which converts it into servlet content (with .java extension). This process is known as translation.
- The web container automatically translates the JSP page into a servlet. So as a developer, you don't have to worry about converting it manually.
Compilation Phase
- In case the JSP page was not compiled previously or at any point in time, then the JSP page is compiled by the JSP engine.
- In this compilation phase, the Translated .java file of the servlet is compiled into the Java servlet .class file.
Initialization Phase
In this Initialization phase: Here, the container will:
- Load the equivalent servlet class.
- Create instance.
- Call the jspInit() method for initializing the servlet instance.
This initialization is done only once with the servlet's init method where database connection, opening files, and creating lookup tables are done.
Execution Phase
This Execution phase is responsible for all JSP interactions and logic executions for all requests until the JSP gets destroyed. As the requested JSP page is loaded and initiated, the JSP engine has to invoke the _jspService() method. This method is invoked as per the requests, responsible for generating responses for the associated requests, and responsible for all HTTP methods.
Destruction(Cleanup) Phase
This destruction phase is invoked when the JSP has to be cleaned up from use by the container. The jspDestroy() method is invoked. You can incorporate and write cleanup codes inside this method for releasing database connections or closing any file.