Servlet and JSP life cycle interpretation

Source: Internet
Author: User
Tags jboss

First, the work of the Servlet project

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.


second, 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.


Third, the JSP life 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.

The JSP life cycle includes the following stages
    • Parsing phase

The servlet container parses the JSP file code and returns an error message to the client if there is a syntax error

    • Translation phase

Servelet container to translate JSP files into Servelet source files

    • Compile phase

Servelet container compiles servelet source files, generates Servelet

    • Initialization phase

Load the Servelet class that corresponds to the JSP, create an example, and invoke its initialization method

    • Run-time phase

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

    • Destruction phase

Call the Servelet instance of the JSP corresponding to the destruction method, and then destroy the Servelet real


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

1. 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.

2. 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.


3. 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.
4. 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}
Iv. code Examples

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >run and see the results later

Turn off the eclipse from above and turn it back on with Firefox or another browser

found that the number of responses to customer requests and above is connected, indicating that both browsers are accessing the same JSP instance.

You can also see the results by printing.

Lin Bingwen Evankaka original works. Reprint please specify the source Http://blog.csdn.net/evankaka

Servlet and JSP life cycle interpretation

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.