Wednesday, October 8, 2014

Lifecycle of Java Server Pages

In this article, we look into the life-cycle of Java Server pages (JSP) along with its phases descriptions. Also, we see the flow of JSP handling the request and in what phases it goes and what would happened at the end after serving request.


Java Server Pages
Java Server Pages (JSPs) are a simple but powerful technology used most often to generate dynamic HTML on the server side. They are a direct extension of Java servlets with the purpose of allowing the developer to embed Java logic directly into a requested document. Using JSP Expression Language, you can develop powerful dynamic web pages powered by Java servlets without any Java code. A JSP document must end with a .jsp extension.

JSP page is processed in several phases during its lifecycle. Following table will describes all the phases of the JSP lifecycle.
The first time the file is requested, it is translated into a servlet and then compiled into a servlet class that is loaded into resident memory. The JSP page then becomes a standard Java servlet, and it goes through the same lifecycle steps as servlet go: the servlet is instantiated, initialized,  and it finally starts to service the client requests until it’s destroyed. After the loaded JSP servlet services each request, the output is sent back to the requesting client.
The servlet generated from the JSP file implements javax.servlet.jsp.HttpJspPage interface, which is responsible for its lifecycle. This interface is very similar to the servlet lifecycle, but with JSP-specific features. The lifecycle methods of the HttpJspPage interface correspond with the Servlet interface methods: 

  • HttpJspPage.jspInit(..) for jsp servlet initialization
  • HttpJspPage.jspService(..) for request servicing 
  • HttpJspPage.jspDestroy(..) for jsp servlet destruction.

Steps of JSP request
On all subsequent requests, the server checks to see whether the original .jsp source file has changed. If it has not changed, the server invokes the previously compiled servlet object, skipping the translation and compilation phases. If the source has changed, however, the JSP engine re-parses the JSP source, going to all phases of the JSP lifecycle.

Note An essential point to remember about JSPs is that they are just servlets that are created from a combination of HTML and Java source. Therefore, they have the same resources and functionality of a servlet.



If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

1 comment: