Home > Java > javaTutorial > body text

When are servlets generally destroyed?

little bottle
Release: 2020-09-16 15:19:17
Original
5780 people have browsed it

Servlet is the abbreviation of Java Servlet. It is called a small service program or service connector. It is a server-side program written in Java. It has the characteristics of being independent of platform and protocol. Its main function is to browse and generate data interactively and generate dynamic Web content, which is typically used to run on the client, results in services such as performing calculations for the user or positioning graphics based on user interaction. When is the servlet destroyed?

When are servlets generally destroyed?

When the server no longer needs the Servlet instance or is reloaded, the destroy method will be called to destroy the servlet. Using this method, the Servlet can release all applications applied for in the init method. resource. Once a Servlet instance is terminated, it is not allowed to be called again and can only wait to be uninstalled.

By the way, let me recall the life cycle of servlet:

1. Loading and instantiating Servlet

When starting the Servlet container, the container first looks for a configuration file web. xml, this file records the Servlet that can provide services. Each Servlet is assigned a Servlet name, which is the complete Java class file name that the Servlet actually corresponds to. The servlet container creates an instance of each servlet with the autoload option. Therefore, every Servlet class must have a public parameterless constructor.

2. Initialization

When a Servlet is instantiated, the Servlet container will call the init method of each Servlet to instantiate each instance. After executing the init method, the Servlet is in the "Initialized" state. "state. Therefore, once the Servlet is instantiated, the init method will be called. Servlet is not initialized immediately after startup, but after receiving the request. Use ... in the web.xml file to pre-initialize the Servlet.

After the initialization fails, the init() method is executed and a ServletException is thrown. The Servlet object will be recycled by the garbage collector. When the client accesses the server for the first time, the Servlet implementation class is loaded, the object is created and the initialization method is executed. .

3. Request processing

After the Servlet is initialized, it is in a ready state to respond to requests. Each request to a Servlet is represented by a Servlet Request object. The Servlet's response to the client is represented by a Servlet Response object. For a request arriving at a client, the server creates a "request" object and a "response" object specific to the request. Call the service method, which can call other methods to handle the request.

The Service method will be called when the server is accessed. The service method may be called multiple times during the life cycle of the Servlet object. Since the web-server is started, some of the resources exposed in the server will be in the network. When the network If different hosts (clients) in the server concurrently access the same resource in the server, the server will open multiple threads to handle different requests. When multiple threads process the same object at the same time, errors in concurrent data access may occur.

Also note that when multiple threads inevitably process the same variable at the same time (such as writing to the same file), and when there are read and write operations, you must consider whether to add synchronization. When adding synchronization, do not add excessive ranges. If it is large, it is possible for the program to become a pure single-thread, which greatly weakens the system performance; it only needs to be safe for multiple threads to access the same object.

4. Destroy Servlet

When the server no longer needs the Servlet instance or is reloaded, the destroy method will be called. Using this method, the Servlet can release all resources applied for in the init method. Once a Servlet instance is terminated, it is not allowed to be called again and can only wait to be uninstalled.

Once the Servlet is terminated, the Servlet instance can be garbage collected and is in the "uninstalled" state. If the Servlet container is closed, the Servlet will also be uninstalled. A Servlet instance can only be initialized once, but multiple identical ones can be created. Servlet instance. For example, the same Servlet can create multiple instances when connecting to different databases according to different configuration parameters.

The above is the detailed content of When are servlets generally destroyed?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!