The servlet life cycle of the Java servlet series

Source: Internet
Author: User

The servlet life cycle defines how a servlet is loaded, initialized, and how it receives requests, responds to requests, and provides services. Before discussing the servlet life cycle, let's look at these methods:

1. Init () method

In the servlet's life cycle, only the init () method is executed once, which is performed when the server loads the servlet, and the server can be configured to get the servlet into the servlet for the first time on the boot server or client. No repetition of Init () is performed no matter how many clients access the servlet;

2. Service () method

It is the core of the servlet, and whenever a client requests a HttpServlet object, the service () method of the object is called and passed to the method a "request" (ServletRequest) object and a "response" (Servletresponse) object as a parameter. The service () method already exists in HttpServlet. The default service function is to invoke the DO function corresponding to the method of the HTTP request.

3. Destroy () method

Executes only once, and executes the method when the server-side stops and unloads the servlet, somewhat similar to the C + + Delete method. A servlet may produce additional threads when running the service () method, so it is necessary to confirm that the threads have been terminated or completed when the Destroy () method is called.

The following is a discussion of the life cycle of the servlet, which is controlled by the servlet container, which begins when the memory of the Web server is mounted and ends when the servlet is terminated or re-loaded. This operation is generally performed dynamically. However, the server typically provides an administrative option to force the loading and initialization of a specific servlet at server startup.

In code, the servlet life cycle is defined by the interface Javax.servlet.Servlet. All Java Servlets must implement the Javax.servlet.Servlet interface directly or indirectly in order to run on the Servlet engine. The Javax.servlet.Servlet interface defines methods that, in the lifetime of the servlet, are invoked in a certain order at a particular time.

Servlet life cycle

Loading and instantiating Servlets

Let's take a look at how Tomcat is loaded:

1. If the automatic mount option is configured, it is automatically loaded at startup.

2. At server startup, the client first makes a request to the servlet.

3. When the servlet is re-loaded.

When the servlet container is started, the container first looks for a configuration file, Web. XML, which records the servlet that can serve the service. Each servlet is assigned a servlet name, which is the full class file name of the Java that the servlet actually corresponds to. The servlet container creates an instance for each servlet that automatically loads the option. Therefore, each servlet class must have a public parameterless constructor.

Initialization

When the servlet is instantiated, the servlet container invokes each servlet's Init method to instantiate each instance, and after the Init method is executed, the servlet is in the initialized state. So, once the servlet is instantiated, the Init method will be called. The servlet is not initialized immediately after startup, but is received after the request. Pre-Initialize the servlet with <load-on-statup> ... </load-on-statup> in the Web. xml file.

After initialization fails, the Execute init () method throws a Servletexception exception, the Servlet object is reclaimed by the garbage collector, the Servlet implementation class is loaded when the client accesses the server for the first time, the object is created, and the initialization method is executed.

Request Processing

After the Servlet is initialized, it is in a ready state to respond to the request. Each request to the servlet is represented by a servlet request object. The servlet's response to the client is represented by a servlet response object. For requests that arrive at the client, the server creates a request-specific request object and a response object. Call the service method, which can call other methods to process the request.

The service method is called when the server is accessed, and the service method may be called multiple times during the lifetime of the Servlet object, because some of the resources exposed in the server will be in the network after Web-server is started. When different hosts (clients) in the network access the same resource in the server concurrently, the server will open multiple threads to handle different requests, and there may be errors in concurrent access to data when multithreading simultaneously processes the same object.

Also note that multithreading is unavoidable when processing the same variable (such as: write to the same file), and read and write operations, you must consider whether to add synchronization, synchronous addition, do not add the scope is too large, it is possible to make the program pure single thread, greatly weakening the system performance It is only necessary to make multiple threads secure access to the same object.

Uninstalling a servlet

The Destroy method is called when the server no longer needs a servlet instance or reload, and the servlet can release all resources requested by the Init method. Once a servlet instance is terminated, it is not allowed to be called again, only to wait for it to be uninstalled.

Once the servlet terminates, the servlet instance can be garbage collected, in an "unloaded" state, and if the servlet container is closed, the servlet is unloaded, and a servlet instance can be initialized only once, but multiple identical servlet instances may be created. As with the same servlet, you can create multiple instances when you connect different databases based on different configuration parameters.

PS: Now use is not many, can only stay on the theoretical level, the following need to use the servlet in practice, in-depth experience in the life cycle of the various methods of the call process.

Reference:

1. Definition of servlet and its life cycle

2. Diagram of the servlet life cycle

The servlet life cycle of the Java servlet series

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.