So, lets get started!!!
The JSP Life-Cycle
The JSP page lifecycle is on the exam. A JSP's life starts when it is first requested. The Servlet Container parses the JSP, creates a servlet, runs the servlet, handles responding to the request, and manages servlet persistence. Smart containers place the servlet in memory only once upon the first request to speed up processing.
Though the life of a JSP looks simple, you might find it better if I put it all in a table for you to refer quickly. Well here we are:
|JSP Life-Cycle Step||Description|
|Page translation||< % = expression % > Translates this to a string. It is equivalent to out.print(expression);.|
|Page compilation||< % yourCode % > Compiles this.|
|Load class||Loads the JSP page's servlet class upon first request.|
|Create instance||Instantiates an instance of the servlet class.|
|Call jspInit||Initializes the servlet instance by calling the jspInit method.|
|Call _jspService||Invokes the _jspService method, passing a request and a response object.|
|Call jspDestroy||If the container needs to remove the JSP page's servlet, it calls the jspDestroy method.|
Each JSP page is eventually converted to a servlet class and then compiled. Each time a request is sent to a JSP page, the container compares the file dates. If the JSP is younger than the servlet, the container recompiles it and then sends the request to the servlet. The compiling process is performed automatically when the server receives a request for that page.
There are two phases of a JSP page's life: translation and execution. In the translation phase, the container starts building the final page, adding contents from any includes and skipping JSP comments (but retaining HTML comments). The container interprets all the Java code, such as directives, actions, and the custom actions referencing tag libraries that occur in the page. Once this is done, the container compiles the result into a servlet class. This completes the translation phase and the servlet is ready to receive requests.
The execution phase involves instantiating request and response objects and invoking the correct servlet based on the request. After the servlet finishes its work, it hands the response object to the container. The container then sends the response back to the client.
The JSP Remains a Static Page Until Requested! The translation and compilation phases can yield errors that are seen only when the page is requested for the first time.
Previous Chapter: Chapter 34 - The Page Directive
Next Chapter: Chapter 36 - JSP Implicit Objects