Life Cycle of JSP

jsp life cycle

A jsp page is not executed directly. it follows a well-defined process that first involves the translation of into its corresponding servlet and then compilation source file class file. resulting translated loaded memory initialized, similar to any other servlet. every time requested, executed. therefore, most stages life cycle are major as follows:

  • The page translation stage
  • The compilation stage
  • The loading and initialization stage
  • The request handling stage
  • The destroying stage (end of service)

Let’s now discuss each stage of the JSP life cycle one by one.

The page translation stage

In the page translation stage of the JSP life cycle, a Web container converts the JSP document into an equivalent Java code, that is, a servlet. Usually, a servlet contains Java code with markup language tags, such as HTML or Extensible Markup Language (XML) embedded within the Java code. JSPS, on the other hand, consists primarily of markup language with some Java code embedded in between the markup tags. Therefore, the objective of page translation is to convert a document chiefly consisting of HTML/XML code, to one that has more of Java code, which can be executed by a Java Virtual Machine (JVM).
In the page translation stage, the Web container performs the following operations:

  • Locate the requested JSP page
  • Validates the syntactic correctness of the JSP page
  • Interpret the standard JSP directives, actions, and custom actions used in the JSP page
  • Writes the source code of the equivalent servlet for the JSP page

After a JSP page is translated into a servlet, all the requests for that JSP page are served by its corresponding servlet class. This servlet class works similar to any other simple servlet. The translation of JSP is done automatically by the Web server. JSP page translation occurs only when necessary, that is, at some point before the page has to serve its first request. This process is also performed when the JSP source code is updated and the page is redeployed. A Web container can decide when the translation should occur. The two possible instances where a translation can occur are as follows:

  • When a first-time user requests a JSP page.
  • When a JSP page is loaded into a Web container. This is also called JSP pre-compilation.

If there is an error in the translation of the page, the container raises the 500 (internal server error) exception and if a change occurs in the JSP page, all the stages of the JSP life cycle are executed again.

The Compilation Stage

The page translation stage is followed by the compilation stage of the JSP life cycle. In the compilation stage, the Java source code for the corresponding servlet is compiled by the JSP container. The container converts the source code into the bytecode, which is included in a class file. After the class file is generated, the container decides to either discard the code or retain it for debugging. Generally, most containers discard the generated Java source code by default. After the compilation stage, the servlet is ready to be loaded and initialized.

The Loading and Initialization Stage

During the loading and initialization stage of the JSP life cycle, the JSP container loads and instantiates the servlet that has been generated and compiled in the translation and compilation stages, respectively. The JSP container, as part of the loading and initialization process, performs the following operations:

  • Loading: Loads the servlet generated during the translation and compilation stages. Normal Java class loading options are used to load the servlet. The loading process is terminated if the Web container fails to load the servlet class.
  • Instantiation: Creates an instance of a servlet class. To create a new instance of the generated servlet, the JSP container uses the no-argument constructor. The JSP translator (part of the JSP container) is responsible for including a no-argument constructor in the servlet generated after the translation of the JSP page.
  • Initialization: Initializes the instantiated object after successful instantiation of the JSP equivalent servlet object. As per the servlet’s life cycle, the container initializes the object by invoking the init(ServletConfig) method. This method is implemented by the container by calling the jspInit() method. This indicates that the jspInit() method acts as the init(ServletConfig) method of the servlet.

If the loading and initialization stage is performed successfully, the Web container activates the servlet object and makes it available and ready to handle client requests.

A JSP page equivalent servlet instance put into service by a container may not handle any request in its lifetime.

The Request Handling Stage

During the request handling stage of the JSP life cycle, only those objects of a JSP equivalent servlet that are initialized successfully are used by the Web container to handle client requests. The container performs the following operations in the request handling stage:

  • Creates the ServletRequest and ServletResponse objects. If a client uses the HTTP protocol to make a request, the container creates the HttpServletRequest and HttpServletResponse objects, where the request object corresponds to the client request. In other words, the request data and client information can be retrieved by a servlet by using the request object. The response object can be used by the servlet to generate the response to a client.
  • Passes the request and response objects created in the preceding step to the servlet object to invoke the service() method. In the case of a JSP equivalent servlet, the container invokes the _jspService() method.

The Destroying Stage

If a servlet container decides to destroy a servlet instance of a JSP page, the container needs to A end the services provided by the instance. The servlet container performs the following operations to destroy the servlet instance:

  • Allows all the currently running threads in the service() method of the servlet instance to complete their operations. Meanwhile, the servlet container makes the servlet instance unavailable for new requests.
  • Allows the servlet container to invoke the destroy() method on the servlet instance, after the current threads have completed their operations on the service() method. The destroy() method leads to the invocation of the jspDestroy() method.
  • Releases all the references of the servlet instance and renders them for garbage collection, after the destroy() method is processed. In addition, the servlet’s life cycle is complete after the invocation of the destroy() method.
  • With this, we complete our discussion on the JSP life cycle. There are various basic tags and implicit objects that constitute the JSP technology.

Era Innovator

Era Innovator is a growing Technical Information Provider and a Web and App development company in India that offers clients ceaseless experience. Here you can find all the latest Tech related content which will help you in your daily needs.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.