The life cycle of Servlets and JSPs

Source: Internet
Author: User

Tag: The source runs the tle call also has a presentation program storage unit

First, the servlet life cycle

A servlet is a servlet container (sometimes called a servlet engine) that is part of a Web server and application server that provides network services on top of sent requests and responses, decodes MIME-based requests, and formats MIME-based responses. The common Tomcat, JBoss, and WebLogic are all servlet containers, and their lifecycle is managed by the container. The life cycle of the servlet is represented by the Init (), service (), and Destroy () methods in the Java.servlet.Servlet interface. The life cycle of a servlet has four phases: load and instantiate, initialize, request processing, destroy.

Load and Instantiate

The servlet container is responsible for loading and instantiating Servelt. The servlet instance is created when the servlet container is started, or when the container detects that the servlet is needed to respond to the first request. When the servlet container is started, the servlet loads the servlet class through the ClassLoader and then initializes a new Servlet object to complete the instantiation.

Initialization

After the servlet is instantiated, the container calls the Init () method and passes the object that implements the ServletConfig interface. In the Init () method, the servlet can read configuration parameters in the deployment descriptor, or perform any other one-time activities. The init () method is only called once for the entire life cycle class of the servlet.

Request Processing

When the servlet is initialized, the container is ready to process the client request. When the container receives a request for this servlet, it invokes the servlet's service () method and passes the request and response objects as parameters. When a parallel request arrives, multiple service () methods can run concurrently in a separate thread. By parsing the ServletRequest or HttpServletRequest object, the service () method processes the user's request and invokes the Servletresponse or HttpServletResponse object to respond.

Destroyed

Once the Servlet container detects that a servlet is to be unloaded, it may be because the resource is being reclaimed or because it is being closed, the container will call the servlet's Destroy () method after all the service () threads of the servlet. The servlet can then collect cleanup for useless storage units. The Servlet object is then destroyed. These four phases collectively determine the life cycle of the servlet.

Second, the JSP life cycle

The JSP page must be converted to a servlet to service the request, so the underlying JSP is completely servlet. In this way, the life cycle of the JSP consists of six stages: conversion, compilation, loading and instantiation, Jspinit () invocation (that is, initialization), _jspservice () invocation (that is, request processing), Jspdestroy () invocation (that is, destruction).

conversion : The Web container transforms the JSP file into a Java source file that contains the definition of the servlet class.

compile : Compile the Java source files created during the conversion phase into class files.

The other four phases of the JSP lifecycle are the same as the servlet life cycle.

Now, let's be specific about:

Previous section:

1. Create: just create a JSP source file

2. Deploy: install JSP on the server (JAVEEE server or single-machine running JSP server)

3. Interpretation and Compilation: the JSP source file is interpreted as a Java source code file, then the source file is compiled into a Java class that can run on the server, this class becomes the implementation class of the JSP page. (This step may be encountered in many places later)

The latter part:

1. Loading and instantiation: The prerequisite is to complete a few steps in the previous section to get an implementation class. The server side will find the existing implementation class for the JSP page, and if not, load the implementation class into the JVM, and immediately generate an instance of the class when loaded into the JVM, or execute it on the first request.

2. Initialize: Initialize the JSP Page object. If you want to execute some code during initialization, you can add an initialization method to the page and invoke the method at initialization time.

3. Request Processing: The Page object responds to the client's request. It is important to note that a single object instance will handle all requests. After processing is completed, the server returns a response (response) to the client. This response is entirely composed of HTML tags and other data, and does not return any Java source code to the client.
4. End of life cycle : The server no longer sends the client's request to the JSP. All instances of this class are freed after all request processing has been completed. This typically happens when the server shuts down, but there are other possibilities, such as the server needs to save resources, detect JSP file updates, or, for some other reason, terminate the instance. If you want your code to perform cleanup work, you can implement a method and call it before the class instance is freed.

The life cycle of Servlets and JSPs

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.