Servlet (2) Life cycle

Source: Internet
Author: User

Various stages of the servlet life cycle

1, creating objects

Creating an object is created only when the user accesses the Web server for the first time, and the Servlet object already exists for the second or more times, so it is no longer created.


Object creation Principle:



When a user accesses the Web server for the first time, the server passes the URL address entered by the user. Parsing the Web. xml file, obtaining the full class name of the Servlet object, and creating the Servlet object through the reflection mechanism, when the server is accessed again, because the Servlet object already exists, it is no longer created, and the Servlet object already exists in the container directly.


2, Initialize

At this point, the Servlet object has been created. Invokes the Init () method of the Servlet object for initialization. Initialization is done after the object is created and executed only once.

3, processing requests

Is the core, calling the servlet's service () method to process the request. Executable multiple times.


4, Object destruction

When the Servlet object is not accessed for a long time, or the container is closed, the Destroy () method is called before destroying to prepare for destruction.


Summary: Servlets are single-instance, initialized once, and have thread-safety issues.

The life cycle of the servlet is managed by the Web container, the programmer cannot change its control, and it is emphasized that our job is to complete the implementation of the methods in the servlet.


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.