The key to understanding JSP's underlying functions in the JSP lifecycle is to understand the lifecycle they follow

Source: Internet
Author: User

JspLife cycle

The key to understanding the underlying functionality of the JSP is to understand the life cycle that they follow.

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

Here are a few stages of the JSP life cycle:

    • Compile phase:

      servlet container compiles servlet source file, generates Servlet class

    • Initialization phase:

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

    • Implementation phase:

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

    • Destruction phase:

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

Obviously, the four main stages of the JSP life cycle are very similar to the servlet life cycle, as shown below:

JSP compilation

When the 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, compile the JSP file.

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 calls the Jspinit () method before providing any services for the request. If you need to perform a custom JSP initialization task, the Jspinit () method is OK, just like this:

public void Jspinit () {  //Initialize code}

In general, the program is initialized only once, and so is 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 of requests in the JSP lifecycle until it is destroyed.

When the JSP Web page finishes initializing, 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 responsible for generating responses to all 7 HTTP methods, such as GET, POST, delete, and so on.

JSP cleanup

The destruction phase of the JSP life cycle 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 closing a folder, and so on.

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

public void Jspdestroy () {   //cleanup code}

The key to understanding JSP's underlying functions in the JSP lifecycle is to understand the lifecycle they follow

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.