Home  >  Article  >  Java  >  Servlet life cycle 5 stages

Servlet life cycle 5 stages

Guanhui
GuanhuiOriginal
2020-06-03 14:56:064537browse

Servlet life cycle 5 stages

The 5 stages of the Servlet life cycle

1. Loading: The Servlet container (Tomcat) loads the servlet file (.class) through the class loader )

2. Creation: Create a servlet object by calling the servlet constructor

3. Initialization: Call the init method to initialize

4. Process customer requests: Whenever there is a Upon customer request, the container will create a thread to handle the customer request

5. Unloading: Call the destroy method to let the servlet release the resources it occupies

The servlet container is responsible for loading the Servlet class and creating Servlet object and instantiate it, then call the Servlet's init method for initialization, and then call the Service method. Instantiation and initialization are different. Instantiate first, then initialize.

ServletConfig and ServletContext

When the Jsp/Servlet container initializes a Servlet type object, it will create a ServletConfig object for the Servlet object. The ServletConfig object contains Servlet initialization parameter information.

ServletContext is the interface for direct communication between servlet and servlet container. When the Servlet container starts a Webapp, it will create a ServletContext object for it, that is, the servlet context environment. Each webapp has a unique ServletContext object. All servlet objects in the same webapp share a ServeltContext, and the servlet object can access various resources in the container through the ServletContext.

In terms of scope, ServletConfig acts on a specific Servlet, that is, it becomes effective when instantiated from the Servlet, but other Servlets other than the Servlet cannot access; ServletContext acts on a certain webapp , which is equivalent to a global object in a webapp, and has been loaded when the Servlet container starts. For different webapps, there are different ServletContexts.

Recommended tutorial: "Java Tutorial"



The above is the detailed content of Servlet life cycle 5 stages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn