JSP is a technology which enables the generation of dynamic web contents for a typical web application by separating the user interface generation from the actual content generation. This separation helps the page designers and content developers to do their respective tasks without interfering with each other. JSP can also be extended by adding Custom Tag Libraries to it.A JSP page is first converted into a Servlet and then that Servlet gets compiled and executed like any other Servlet. The translation of a JSP page into the corresponding Servlet is done by the JSP Engine of the underlying Web Container. The Entire life cycle of a typical JSP page can be summarized as the following phases Stage 1: JSP Page Translation: A java Servlet file is generated from the JSP source file. This is the first step in its 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. Stage 2: JSP Page Compilation: Once the JSP page has been translated into the corresponding Servlet, the next obvious step is to compile that Servlet. The translation of a JSP source page into its implementation class can happen at any time between initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for the target JSP page. Stage 3: Class Loading & Instantiation Phase: The java servlet class that was compiled from the JSP source is loaded into the container. Once the class is loaded, an instance of this class gets created. JspPage interface contains the jspInit() method, which is used by the JSP Engine to initialize the newly created instance. This jspInit() method is just like the init() method of the Servlet and it's called only once during the entire life cycle of a JSP/Servlet. Stage 4: Servicing Requests: 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() method cannot be overridden. Every time a request arrives, the main JSP thread spawns a new thread and passes the request (incoming request) and response (new) objects to the _jspService() method which gets executed in the newly spawned thread. Stage 5: jspDestroy() Phase: jspDestroy() method is called (almost always by the Web Container)Read a related article on why a JSP/Servlet writer shouldn't call this method explicitly. 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. Here we are going to describe the jsp life cycle through an example Step 1: Create A New Web Application For creating a new application select java web and click web application, after click on the next button Step 2: Name and Location Give a specific Name and Choose a specific Location for it Step 3: Server And Setting: Select server Glassfish 3.1 and java web version Step 4: Select Framework: There is no need to select any framework for it Step 5: Create Jsp File: Create two jsp file index.jsp and string.jsp Index.jsp <HTML> <HEAD> <TITLE>Using jspInit and jspDestroy</TITLE> </HEAD> <BODY bgcolor="cyan"> <H1>Using jspInit and jspDestroy</H1> <%! int number; public void jspInit() { number = 50; } public void jspDestroy() { number = 0; } %> <% out.println("The number is " + number + "<BR>"); %> </BODY> </HTML> String.jsp <HTML> <HEAD> <TITLE>Using jspInit and jspDestroy</TITLE> </HEAD> <BODY bgcolor="pink"> <H1>Using jspInit and jspDestroy</H1> <%! String s; public void jspInit() { s = "Vikas Mishra"; } public void jspDestroy() { s = "0"; } %> <% out.println("The String is " + s + "<BR>"); %> </BODY> </HTML> Compiling and Running The application After compiling and Running the above code we found the following output Output index.jsp String.jsp
Throwing an Exception in a JSP Page
Working With Inheritance Concept in JSP
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 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. Class Loading: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: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() are called the life cycle methods of the JSP.
In the JSP lifecycle, the destroy method is also invoked when the server shuts down http://www.javaexperience.com/understanding-jsp-lifecycle