JSP page life cycle is a process of converting the JSP page into a servlet or in other words, you can say a web container generates a separate file with a (_jsp.java) extension. this file contains the code of Java that required compilation and execution.
JSP allows writing the HTML and Java code in single file. To understand the JSP life cycle, will recommend checking the JSP tutorial, What is JSP?.
JSP is known as Java Server Page. Another Java technology to develop dynamic web applications. JSP adds Java code inside HTML using JSP tags. We can write both server-side and client-side code in a JSP file.
- JSP allows us to write HTML code containing tags, inside which we can include business logic in Java.
- Using JSP, one can easily separate Presentation and Business logic as a web designer can design and update JSP pages creating the presentation layer.
Stages Involved in the Life Cycle of JSP
- Translation or convert JSP into Servlet code.
- Compilation of Servlet to bytecode.
- Loading Servlet class.
- Creating servlet instance.
- Initialization by calling jspInit() method.
- Request Processing by calling _jspService() method.
- Destroying by calling jspDestroy() method.
JSP is an extended version of servlet so the life cycle of JSP is also the same as a servlet lifecycle but the difference is only the translation face. The main component responsible here is JSP Engine.
What is JSP Engine?
Java Server Pages(JSP) engine is the implementation of JavaServer pages specifications. Its the JSP Engine’s responsibility to translate, execute, and process the JSP page.
Let us see every step in detail:
Translation of JSP
This is the first step involved in the life cycle of JSP. Here, the .jsp file is converted into .java file.In this phase, the JSP Engine validates the correctness of the JSP page.
Compiling JSP Page
Now, after the translation of the JSP page, we have a second phase called Compiling of JSP Pages. Here, the .java file from the translation page is converted to a .class file. The Servlet Container is responsible to compile the.java file.
Loading Servlet Class
Here, the Servlet class which has been generated is now loaded into the container.
Instantiation
After loading of Servlet Class into the container, an instance of the class is created. The container will manage the object of the class.
Initialization
In this phase, the object is initialized. The _jspinit() method is called only once after the Servlet class instantiation. The _jspinit() method is called only once during the life cycle of JSP.
public void jspInit() { //initialize the given code }
Request Processing
The actual processing of the request is done in this step. The _jspservice() method handles the request by JSP. It is called multiple times.
void _jspservice(HttpServletRequest req HttpServletResponse res) { //handle all request and responses }
Destruction
This is the last stage in the life cycle of JSP. Here, the _jspdestroy() method is called to remove the instance of the servlet class. It is called only once.
public void _jspdestroy() { //destruction }
_jspdestroy() method can be called if you want to close an open files, releasing database connection. It can be overridden.
JSP Lifecycle Example
Now, we can take an example for this Like I have (hello. jsp) file with the following steps.
- Web container translate this (hello.jsp) file in (.java) file.
- Compile into a java servlet class and generate a bytecode(.class) file.
- Bytecode loaded by the class loader.
- The container creates an instance of that servlet class.
- Now for service request initialized servlet by jspInit().
- Web container call _jspService() method for each request.
- In the end jspDestroy() destroy the request.
If you want to know the life cycle of the JSP page in-depth do check this article https://codedec.com/tutorials/life-cycle-of-jsp/
Now, let us see the Architecture of JSP in detail:
JSP Architecture
Here, we will discuss the Architecture of JSP i.e Java Server Pages in detail. JSP Engine plays a vital role in JSP Architecture.
JSP Engine
As Servlet needs a Servlet container to run Servlet Programs Just like that For JSP pages to run we need a JSP Engine i.e a Container to process JSP pages. JSP Engine i.e a container works in combination with WEB Server to provide an Environment for the JSP page. Its the JSP Engine’s responsibility to translate, execute, and process the JSP page.
Now, let us see the processing of the JSP page from the following steps:
- First, the client i.e your browser sends the HTTP request to the webserver.
- Now, the Web Server reads the request. If the request is for a .jsp file then it will forward it to JSP Engine.
- Now, it’s JSP Engine’s responsibility to convert it into servlet content. (all the JSP elements are converted to Java Code)
- With the help of the Compiler, the Servlet code is now converted to executable code and forwarded to Servlet Container.
- Now, the loading and executing of the Servlet class are done by the Servlet container and after this, the response is generated in the HTML format and is sent to the webserver.
- Now, the Web Server forwards the response to the client.
- At last, the client(the web browser) will handle the dynamically generated HTML page inside the HTTP response.
Thus, this was all about the Architecture of a JSP page which is almost similar to Servlet only the difference here is in the translation phase.
In this way, we learned the life cycle and Architecture of JavaServer pages in detail. Further, if you like to read some more detail on this particular topic do check this article https://codedec.com/tutorials/introduction-of-jsp-in-java/
Practise Task in JSP
- Inserts a date in the database using date picker, JSP
- Insert Data of multiple pages in a single table.
- Insert data from the single page into multiple tables.
- login and logout with JSP and servlet with Mysql
- JSP page directives Jsp, Include Directive, JSP Taglib directive
- Include Directive in JSP
- Implicit Objects in JSP with example
- How to count the number of visitors for the website in JSP
- Java database connectivity with MYSQL 8.0