Explain the life cycle of a JSP page?
There are two phases in the life cycle of a JSP:
- Translation and Compilation Phase
- Execution Phase
During the translation and compilation phase the container creates a servlet for the JSP from the source file. A JSP page services client requests as a servlet. When a request is mapped to a JSP page, the web container first checks whether the JSP page’s servlet exists and if it exists, whether it is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During this phase, the container interprets the scripting elements, directives and actions, and the custom actions referencing tag libraries used in the page.
During the execution phase the JSP container executes the servlet generated from the JSP page. If an instance of the JSP page’s servlet does not exist, the container:
- Loads the JSP page’s servlet class
- Instantiates an instance of the servlet class
- Initializes the servlet instance by calling the jspInit method
The container invokes the _jspService method, passing request and response objects. If the container needs to remove the JSP page’s servlet, it calls the jspDestroy method.