Lifecycle of Java Web Applications and Servlets

The life cycle of a Java Web application is controlled by the servlet container. Includes three stages:

1. Startup phase: Load the relevant data of the Web application, create the ServletContest object, and initialize the Filter (filter) and some Servlets. At startup it will:

  (1) Load the data in the web.xml file into memory.

  (2) Create a ServletContext object for the Java Web application.

  (3) Initialize all Filters.

  (4) Initialize the Servlet that needs to be initialized when the Web application starts.

2. Operation phase: provide services to clients.

  The most important declaration stage of Java Web application, in this stage, all servlets are on standby, ready to respond to specific requests from clients and provide corresponding services. If the servlet requested by the client does not exist, the servlet container will initialize the servlet first, and then call its Service method.

3. Termination stage: release various resources occupied by the Web application. When terminated will:

  (1) Destroy all running servlets in the Java Web application.

  (2) Destroy all the Filters in the runtime state in the Java Web application.  

  (3) Destroy all objects related to the Java Web application, such as the ServlerContext object, etc., and release the related resources occupied by the Web application.

 


 

Servlet life cycle

 

  • When the servlet container starts, the Java Web application is started, and when the Java Web application starts, it is in the runtime state. When the servlet container is shut down , the servlet will first terminate all Java Web applications.
  • The life cycle of the Java Web application is controlled by the Servlet container. As the core component of the Java Web application, the servlet's life cycle is also controlled by the Servlet container.
  • The life cycle of a servlet is divided into three phases, initialization phase, runtime phase and destruction phase. Three methods are defined after the javax.servlet.Servlett stage: init(), service(), destroy(), which are called by the servlet container at different stages of the servlet.

1. Initialization stage:

  (1) The servlet container loads the Srvlet class, and reads the data in its .class file into the memory.

  (2) The servlet container creates a ServletConfig object. The ServletConfig object contains the initialization configuration information of a class-specific servlet, such as the initial parameters of the servlet. In addition, the servlet container also associates the ServletConfig object with the ServletContext object of the current Web application.

  (3) The servlet container creates the servlet object.

  (4) The Servlet container uses the init (ServletConfig config) method of the Servlet object, and in the init (ServletConfig config) method of the GenericServlet implementation class of the Servlet interface, the association between the Servlet object and the ServletConfig object will be established.

********* In the initialization phase, a class Servlet object and a ServletConfig object are created, and the Servlet object is associated with the ServletConfig object, and the ServletConfig object is associated with the ServletContext object of the current Web application.

******** Servlet enters the initialization situation:

  • The current web application is in the runtime phase, and a specific servlet is accessed by the client for the first time, and most servlets will be initialized by the servlet container in this case.
  • If the <load-on-startup> element is set for a servlet in the web.xml file, the servlet will be initialized when the servlet container starts the web application to which the servlet belongs.
  • When the web application is restarted, all servlets in the web application will be reinitialized at a specific time.

2. Runtime stage:

  At this stage, the servlet can respond to the client's request at any time. When the servlet container receives a client request to access a specific servlet, the servlet container will create a ServletRequest object and a ServletResponse object for the request, and then call the servlce() method of the corresponding servlet object. The service() method obtains the client request information from the ServletRequest object and processes the request, and then generates the response result through the ServletResponse object. When the servlet container sends the response result generated by the servlet to the client, the servlet container destroys the SevletRequest object and the ServletResponse object

3. Destruction stage:

  When the web application is terminated, the servlet container will first call the destroy() method of all servlet objects in the web application, and then destroy these servlet objects. In the implementation of the destroy() method, the resources occupied by the servlet can be released (such as closing the file input and output streams, closing connections to databases, etc.), and also destroys the ServletConfig object associated with the Servlet object.

 

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324646705&siteId=291194637
Recommended