JSP Architecture + Lifecycle

Java Server Pages are part of a 3-tier architecture. A server(generally referred to as application or web server) supports the Java Server Pages. This server will act as a mediator between the client browser and a database. The following diagram shows the JSP architecture.

JSP architectureFigure 1: JSP Architecture

JSP Architecture Flow

  1. The user goes to a JSP page and makes the request via internet in user’s web browser.
  2. The JSP request is sent to the Web Server.
  3. Web server accepts the requested .jsp file and passes the JSP file to the JSP Servlet Engine.
  4. If the JSP file has been called the first time then the JSP file is parsed otherwise servlet is instantiated. The next step is to generate a servlet from the JSP file. The generated servlet output is sent via the Internet form web server to users web browser.
  5. Now in last step, HTML results are displayed on the users web browser.

JSP page in a way is just another way to write a servlet without having to be a Java programming expert. Except for the translation phase, a JSP page is handled exactly like a regular servlet. Now that we saw a big picture in JSP architecture section, lets dive a more deeper and understand how a JSP file is treated in a container and what phases it passes through.

JSP Lifecycle

In this section we will discuss about each phase of a JSP execution cycle. A JSP life cycle is similar to a servlet life cycle with an added step wherein you need to compile a JSP into a servlet. JSP pages are usually managed by a web container which normally contains a servlet container and a JSP container.

JSP LifeCycleFigure 2: JSP Life Cycle

The following table lists each phase of JSP life cycle with a description:

PhaseDescription
TranslationJSP container parses the JSP pages. It then translate the JSP pages to generate corresponding servlet source code. If JSP file name is hello.jsp, usually it is named as hello_jsp.java by the container.
Page CompilationIf the translation is successful, the generated java file is then compiled by the container.
Class LoadingOnce JSP is compiled as servlet class, its lifecycle is similar to servlet. The compiled class is then loaded into the memory.
Instance CreationOnce JSP class is loaded into memory, its object is instantiated by the container.
Call jspInit() or InitializationDuring this phase the JSP class is initialized transformed from a normal class to servlet. Once initialization is over, ServletConfig and ServletContext objects become accessible to JSP class. Method jspInit() is called only once in JSP lifecycle. It initializes config params.
Call _jspService or Request ProcessingThis method is called for each client request.
Call jspDestroy or DestroyThis is the last phase and this method is called when the container decides to unload JSP from memory.

Previous Tutorial : Introduction to JSP   ||  Next Tutorial : JSP Example using Eclipse

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*