JSP life cycle

Source: Internet
Author: User

The key to understanding the JSP's underlying functionality is to understand the lifecycle that they follow.

The JSP lifecycle is the entire process from creation to destruction, similar to the servlet lifecycle, except that the JSP lifecycle also includes compiling JSP files into servlet.

Here are a few phases of the JSP lifecycle:

  Compile phase:

servlet container compiles servlet source file, generates Servlet class

  Initialization phase:

Load the servlet class corresponding to the JSP, create the example, and call its initialization method

  Implementation phase:

Invoking the service method for the servlet instance corresponding to the JSP

  Destruction phase:

Invokes the Destroy method of the servlet instance corresponding to the JSP, and then destroys the servlet instance

Obviously, the four major phases of the JSP lifecycle are very similar to the servlet lifecycle, as shown in the following illustration:

JSP compilation

When a browser requests a JSP page, the JSP engine first checks to see if the file needs to be compiled. If the file has not been compiled, or has been changed since the last compilation, the JSP file is compiled.

The compilation process consists of three steps:

Parse the JSP file.

Convert the JSP file to a servlet.

Compile the servlet.

JSP initialization

After the container loads the JSP file, it invokes the Jspinit () method before providing any services for the request. If you need to perform a custom JSP initialization task, the Replication Jspinit () method is OK, as follows:

public void Jspinit () {

Initializing code

}

In general, the program is initialized only once, as is the case with the servlet. Typically you can initialize a database connection, open a file, and create a query table in the Jspinit () method.

JSP execution

This phase describes all the interaction behaviors associated with the request in the JSP lifecycle until it is destroyed.

When the JSP Web page completes initialization, the JSP engine calls the _jspservice () method.

The _jspservice () method requires a HttpServletRequest object and a HttpServletResponse object as its arguments, as follows:

void _jspservice (HttpServletRequest request,

HttpServletResponse response)

{

Service-Side processing code

}

The _jspservice () method is called once in each request and is responsible for generating the corresponding response, and it is also responsible for generating responses from all 7 HTTP methods, such as GET, POST, delete, and so on.

JSP cleanup

The destruction phase of the JSP lifecycle describes everything that happens when a JSP page is removed from the container.

The Jspdestroy () method is equivalent to the destruction method in the servlet in the JSP. The replication Jspdestroy () method when you need to perform any cleanup work, such as releasing a database connection or shutting down a folder, and so on.

The format of the Jspdestroy () method is as follows:

public void Jspdestroy ()

{

Cleanup code

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.