Javaweb application and servlet life cycle

Source: Internet
Author: User

The life cycle of the Javaweb application is controlled by the servlet container. Consists of three stages:

1. Start-up phase: load the Web app's data, create a Servletcontest object, initialize the filter (filter) and some servlets. When it starts:

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

(2) Create a ServletContext object for the Javaweb app.

(3) Initialize all filter.

(4) Initialize the servlet that needs to be initialized when the Web app starts.

2. Run phase: provide services to clients.

Javaweb application The most important declaration phase, at this stage, all servlets are on standby, ready to respond to the client's specific request, and provide the appropriate services. If the servlet requested by the client does not exist, the servlet container initializes the servlet and then calls its service method.

3. Termination phase: Release the various resources occupied by the Web application. Upon termination:

(1) Destroy all running Servlets in the Javaweb application.

(2) Destroy all the filter in the Javaweb application that is in the run-time state.

(3) Destroy all objects related to the Javaweb application, such as Servlercontext objects, and release related resources occupied by the Web application.

the life cycle of the servlet

    • When the servlet container is started, the Javaweb app is started, and when the Javaweb app starts, it is in the run state. When the servlet container is closed, the servlet terminates all javaweb applications first.
    • The life cycle of the Javaweb application is controlled by the servlet container, and the servlet is the core component of the Javaweb application, and its lifecycle is controlled by the servlet container.
    • The life cycle of a servlet is divided into three phases, the initialization phase, the runtime phase, and the destruction phase. After the Javax.servlet.Servlett phase, 3 methods are defined: init (), service (), Destroy (), which are called by the servlet container at different stages of the servlet.

1. Initialization phase:

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

(2) The servlet container creates the ServletConfig object, and the ServletConfig object contains initialization configuration information for the class-specific servlet, such as the initial parameters of the servlet, in addition, The servlet container also causes the ServletConfig object to be associated with the ServletContext object of the current Web application.

(3) servlet container creates a Servlet object.

(4) servlet container with the init (servletconfig config) method of the Servlet object, in the init (servletconfig config) method of the implementation class Genericservlet of the Servlet interface , the association between the Servlet object and the ServletConfig object is established.

********* During the initialization phase, The class Servlet object and the 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 App.

******** servlet entering initialization:

    • The current Web application is in the runtime phase, and the particular servlet is first requested by the client, and most servlets are initialized in this case by the servlet container.
    • If the <load-on-startup> element is set for a servlet in the Web. xml file, this servlet is initialized when the servlet container launches the application to which the servlet belongs
    • When a web app is restarted, all servlets in the Web app are reinitialized at a specific moment.

2. Run-time phase:

  At this stage, the servlet can respond to the client's request at any time, and when the servlet container accepts a request for a client requesting access to a particular servlet, the servlet container creates the ServletRequest object and the Servletresponse object for that request , and then call the Servlce () method of the corresponding Servlet object. The service () method obtains the customer request information from the ServletRequest object and processes the request, and then generates the response result from the Servletresponse object. The servlet container destroys the Sevletrequest object and the Servletresponse object when the servlet container sends the response results generated by the servlet to the client.

3. Destruction Phase:

When the web app is terminated, the servlet container invokes the Destroy () method of all Servlet objects in the Web App before destroying the servlet objects, freeing the resources used by the servlet in the implementation of the Destroy () method ( such as closing the file input stream and output stream, closing the connection to the database, etc.), also destroys the ServletConfig object associated with the Servlet object.

  

Javaweb application and servlet life cycle

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.