JSP Life Cycle

JSP Life Cycle Phases and Methods

In this article, I am going to discuss the JSP Life Cycle Phases and Methods in detail. Please read our previous article where discussed JSP Architecture. At the end of this article, you will understand the following pointers in detail.

  1. What is JSP Life Cycle?
  2. Different Phases of JSP Life Cycle.
  3. JSP Life Cycle Flow of Execution
  4. Different Methods of JSP Life Cycle
  5. Difference between a Web Server, a Web Container, and an Application Server
What is JSP Life Cycle?

JSP Life Cycle is defined as the translation of the JSP Page into the servlet. Because JSP Page always needs to be converted into servlet page first in to process the service requests. Every JSP page ends with .jsp extension. Whenever we were given a request to the server for a .jsp file then first .jsp will be converted into a .java file. Second, .java file is converted into .class file. Third, .class is loaded by container and managed by the container. Basically .jsp to .java and .java to .class conversion is performed by JSP engine. All .class file loading and execution is performed by the container (servlet container). The JSP Life Cycle always starts with the creation of the JSP page and ends with the disintegration of that.

Different Phases of JSP Life Cycle

JSP page life cycle contains the following phases:

Different Phases of JSP Life Cycle

When we send a request from the client to the server for a specific JSP page then the container will devour the request, identify the requested JSP pages, and perform the subsequent life cycle actions:

JSP Loading:

In this phase, the container will load the JSP files to the memory from the web application directory structure.

JSP Parsing:

In this phase, the container will check whether all the tags available on the JSP page are in well-formed format or not.

JSP Translation to Servlet:

When JSP receives the first request from the client, the JSP container translates JSP into Servlet. After the JSP Parsing container will translate the loaded JSP page into a specific servlet. While executing a JSP page Tomcat container will provide the translated servlet within the following location at Tomcat Server.

C:\Tomcat 9.0\work\catalina\localhost\org\apache\Jsp\first_Jsp.java

If the JSP file name is first.jsp then Tomcat Server will provide a servlet with name first_jsp. All the translated servlets provided by Tomcat container are by default final. The default superclass for a translated servlet is HttpJspBase. Where JSPPage interface has declared the following methods:

  1. public void _JspInit()
  2. public void _JspDestroy()

and the HttpJspPage interface has provided the following method:

  1. public void _JspService(HttpServletRequest req, HttpServletResponse res)

For the above three abstract methods, HttpJspBase class has provided the default implementation but the _JspService(_,_) method would be overridden in the first_jsp class with the content that we provided in first.jsp file.

Servlet Compilation

After successful translation of servlet container will compile servlet java file and generates the respective .class file. Here, the source program is compiled and generates byte code or .class file.

Servlet Loading

In this phase, the container will load the translated servlet class byte code to the memory. Here, the compiled .class file is loaded into JVM.

Servlet Instantiation

In this phase, the container will load the translated servlet class byte code to the memory. Here, creates an object of the loaded class.

Servlet Initialization

In this phase, the container will access the _JspInit() method to initialize the servlet. Here, JSP is initialized by calling JspInit() method.

Creating request and response objects

After the servlet is initialized successfully, the container will create a thread to access the _JspService(_,_) method, for this container has to create HttpServletRequest and HttpServletResponse.

Generating Dynamic Response:

After getting request and response objects container will access the _JspService(_,_) method, by executing its content and then the container will generate some response on the response object.

Dispatching dynamic response to the client:

When container generated thread reached the ending point of the _JspService(_,_) method then that thread is going to be in a Dead state, with this container will dispatch dynamic response to the client through the Response Format.

Destroying request and response objects

When the dynamic response reached to client protocol will terminate its virtual socket connection, with this container will destroy request and response objects.

Servlet Deinstantiation

After destroying request and response objects container is going to be in a waiting state depends on the container, then the container identifies no further request for an equivalent resource then the container will destroy the servlet object, for this container will execute the _JspDestroy() method.

Servlet and JSP Unloading:

After the servlet de-instantiation container will eliminate the translated servlet byte code and Jsp code from memory.

Flow of Execution

First, the container loads .jsp file from memory. Then container verifies the syntax of JSP tags placed in the .jsp file. Using the JSP page compiler JSP program will be converted into an equivalent servlet class. The java compiler (javac) compiles the JES class source file. Then container loads JES class and creates the object. Container then calls jspInit() method through init(ServletConfig) method to initialize ServletConfig object with JES class object. Then it creates one set of requests, a response object from the current request. The container then calls the _jspService(_,_) method of JES through service(_,_) to process the request. The output generated by _jspService(_,_) method goes to the browser as a response. Once the response is delivered to the request, the response object will be destroyed. If there is no further request to JSP or if the web application stopped/reloaded the container performs servlet de-instantiation. When this is about to happen container calls jspDestroy() through the destroy() method. JVM then unloads the loaded JES class. At last container unloads the loaded .jsp file.

Different Methods of JSP Life Cycle

The lifecycle of JSP is controlled by three methods which are automatically called, when a JSP is requested and when the JSP terminates normally. These are: jspInit(), _jspService() and jspDestroy()

Different Methods of JSP Life Cycle

jspInit() Method:

jspInit() method is similar to the init() method in a Java Servlet and an applet. It is called first when the JSP is requested and is employed to initialize objects and variables that are used throughout the lifetime of the JSP.

_jspService() Method:

_jspService() method is automatically called and retrieves a connection to HTTP. It will call the doGet or doPost() method of servlet created.

jspDestroy() Method:

jspDestroy() method is similar to the destroy() method in Servlet. The destroy() method is automatically called when the JSP terminates normally. It isn’t called by JSP when it terminates. It is used for cleanup where resources used during the execution of the JSP are released.

Difference between Web Server, Application Server, and Web Container

Web Server
  1. Web Servers are responsible for serving static content e.g., HTML over HTTP protocol.
  2. Apache and IIS are two popular web servers. Apache is used everywhere including Java world but IIS is most popular in Microsoft ASP .NET world.
  3. It is expected from a web server to provide HTTP protocol level service.
  4. We need a web server like Apache HTTPD if we are serving static web pages.
Application Server
  1. Application Server is responsible for serving dynamic content, managing the EJB pool, facilitating the distributed transactions, facilitating application lookup over JNDI, application security, and others.
  2. From the Java EE perspective couple of popular application servers are IBM Websphere, Oracle WebLogic, glassfish, and Redhat’s Joss.
  3. Application Server is supposed to provide more powerful and dynamic Web service and business level service via EJB (Enterprise Java Beans).
  4. If we have a Java EE application using EJB, distributed transaction, messaging and other fancy features then we need a full-fledged application server like JBoss, WebSphere, or Oracle’s WebLogic.
Web Container
  1. Web Containers are only responsible for generating HTML by executing JSP and Servlet on Server Side.
  2. In Web Containers, Apache Tomcat and Jetty are two of the most popular Servlet engines in the java web world.
  3. Essential Services like Database connection pooling is not only provided by application server but also by Web Containers like Tomcat.
  4. If we have a Java application with just JSP and servlet to generate dynamic content then you need web containers like Tomcat or Jetty.

In the next article, I am going to discuss JSP Elements. Here, in this article, I try to explain the JSP Life Cycle Phases and Methods. I hope you enjoy this JSP Life Cycle Phases and Methods article.

Leave a Reply

Your email address will not be published.