Life Cycle of JSP for beginner

Introduction:
JSP is a server-side technology that does all the processing at the server. It is used for creating dynamic web applications, using Java as a programming language.
JSP page is saved with .jsp extension to know the servlet that this is the JSP page to process the JSP accordingly.
In my previous post, I have explained in detail about JSP. If you have not seen that post, do check out. Introduction to JSP tutorial
 JSP Lifecycle Phases: 
1. JSP Page Translation:

 A java servlet file is generated from the JSP source file. This is the first step in its tedious multiple phase life cycle. In the translation phase, the container validates the syntactic correctness of the JSP pages and tag files. The container interprets the standard directives and actions, and the custom actions referencing tag libraries used in the page. 

2. JSP Page Compilation:

The generated java servlet file is compiled into a java servlet class.

Note: The translation of a JSP source page into its implementation class can happen at any time between the initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for the target JSP page. 

3. ClassLoading: The java servlet class that was compiled from the JSP source is loaded into the container.

4. Execution phase:

In the execution phase, the container manages one or more instances of this class in response to requests and other events. The interface JspPage contains jspInit() and jspDestroy(). The JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP requests and this interface contains _jspService(). 

5. Initialization:

 jspInit() method is called immediately after the instance was created. It is called only once during JSP life cycle. 

6. _jspService() execution: 

This method is called for every request of this JSP during its life cycle. This is where it serves the purpose of creation. Oops! it has to pass through all the above steps to reach this phase. It passes the request and the response objects. _jspService() cannot be overridden. 

7. jspDestroy() execution: 

JSP This method is called when this JSP is destroyed. With this call, the servlet serves its purpose and submits itself to heaven (garbage collection). This is the end of jsp life cycle. jspInit(), _jspService() and jspDestroy() is called the life cycle methods of the JSP


 Advantages of JSP over Servlet: 

1. We can generate HTML response from servlets also but the process is cumbersome and error-prone, when it comes to writing a complex HTML response, writing in a servlet will be a nightmare. JSP helps in this situation and provides us the flexibility to write a normal HTML page and include our java code only where it’s required. 

2. JSP provides additional features such as tag libraries, expression language, custom tags that helps in faster development of user views. 

3. JSP pages are easy to deploy, we just need to replace the modified page in the server and the container takes care of the deployment. For servlets, we need to recompile and deploy the whole project again. 

The Servlet and JSPs complement each other. We should use Servlet as a server-side controller and to communicate with model classes whereas JSPs should be used for the presentation layer. 



I hope you enjoyed this post, related to the JSP topic do read other post of JSPs.

Read Topics:

Introduction to JSP Tutorial



Previous
Next Post »