JSP Life Cycle

In this chapter, we will discuss JSP Life Cycle. We will see whole process from creation till destruction.

I hope you’ve unserstood the architecture of JSP and its key components.

Below is the quick overview of JSP Life Cycle:

  1. Translation – JSP page is translated to Servlet and generates (.java) file.
  2. Compilation – Generated file is compiled to (.class) file.
  3. Classloading – class file is loaded into the container.
  4. Initialisation Container invokes jspInit() method.
  5. Processing – Request is served using _jspService() method.
  6. Destroy – File is destroyed from the container using the jspDestroy() method.

JSP Life cycle Diagram:

jsp-life-cycle

Now let’s understand What is going on in the above diagram:

First, your browser will request a JSP page called index.jsp it will be translated to servlet which generated typically (.java) file then this java file is compiled to (.class) file known as Byte Code in Java.

After succussful compilation, (.class) file is loaded into the JSP container using the Class Loaders. Now let’s see what happens inside JSP Engine.

1. jspInit() method:

After loading (.class) file, JSP container invokes jspInit() method for serving the request. This method can be overriden and modified as per the need. But remember, jspInit() is only called once in the entire lifecycle of JSP page.

2. _jspService() method:

This is the main execution phase, It generates the response for the upcoming request. This methods takes HttpServletRequest and HttpServletResponse as a parameter and return the final response.

3. jspDestroy() method:

This methods is used to clean up JSP container when the lifecycle ends. You can override this method and invoke whenever you want to clean up the container.

Frequently Asked Questions:

1. Can we Override jspInit() and jspDestroy() methods?

Yes. jspInit() and jspDestroy() can be overriden but _jspService() can’t.

I hope you’ve understood the Life Cycle of JSP. Navigate to next chapter now.

JSP Architecture

JSP Syntax

Leave a Comment