JSP stands for Java Server Pages. In this tutorial, we will learn about the life cycle of a JSP Page. The JSP Lifecycle includes 7 steps in translating a JSP page into a servlet code. Learn all those steps or phases in detail from here. But, you all need to understand what is JSP life cycle? at first.
This JSP LifeCycle Tutorial includes the following:
- What is the JSP life cycle?
- Steps Involved in JSP life Cycle
- JSP Directory Structure
- Advantages of JSP over Servlet
What is the JSP life cycle?
JSP life cycle is the process of translation of a JSP page into servlet code. The container is liable for translating the JSP into a runnable servlet code. The purpose of the container is to take a JSP file then do various tasks like validation & produce the appropriate servlet code.
The following image is the diagrammatic representation of the JSP life cycle:
Steps Involved in JSP life Cycle
There are seven steps included in translating a JSP page into a servlet code. So, here we will discuss certain paths followed by JSP in the life cycle of the Java Server Page:
1. Translation process: The container translates a JSP page into servlet code in this process.
2. Compilation process: In the compilation process, the java servlet file is compiled into a class file.
3. Loading process: While loading process, the servlet class is loaded into the container.
4. Instantiation process: In this process, an instance of the servlet is generated.
5. Initialization process: JSP Page is initialized by _jspinit() method. It allows the page author to provide the initialization to the JSP Page. This method will redefine the init() method, which is defined in the servlet class. When this method is called then all the methods in servlets, including getServletConfig(), are available. This method is called only once during the JSP life cycle.
6. Request process: The _jspservice () method is used for request processing. This method is invoked by the container. This method is called to handle each request. The response is produced from within this method and then returned to the container so that it can be passed back to the client.
7. Destroy process: The _jspDestroy() method is used to destroy the JSP Page. The destroy() method defined in the servlet class is redefined by this method. If any cleanup is required, then the page author can define this method. This method is called only once during JSP page Life Cycle.
JSP Directory Structure
You have to get an idea about the directory structure of the JSP Page. Here, the JSP directory structure is similar to Servlet. We hold the JSP page outside the WEB-INF folder or in any directory.
Advantages of JSP over Servlet
In the following points, you will observe various benefits of JSP over the Servlet. So, check it out thoroughly:
1) JSP page is the Extension to Servlet technology.
2) Very simple & easy to maintain
3) Fast Development, there is no need to recompile and redeploy.
4) Less code than Servlet