The life cycle of the servlet

Source: Internet
Author: User

1) The servlet runs in the container, and the creation and destruction of its instances are not determined by the programmer, but are controlled by the container.

There are two opportunities to create a servlet instance:

1, when the client first requests a servlet, the system creates an instance of the servlet: Most of the servlets are this servlet.

2,web Create a servlet instance as soon as the app starts, i.e. Load-on-startup 1

The life cycle of the servlet is represented by the Init (), service (), and Destroy () methods in the Javax.servlet.Servlet interface.

Each servlet runs according to the following life cycle.

(1) Loading and instantiation: Find the location of the servlet class loading the servlet class through the ClassLoader, after successful loading, the container creates the servlet instance through the Java Reflection API, invoking the default constructor method of the servlet (that is, the constructor without parameters).

(2) Initialize: The container initializes the object by calling the Servlet's init () method. The purpose of initialization is to have the Servlet object do some initialization work before processing the client request, such as establishing a connection to the database, obtaining configuration information, and so on. For each servlet instance, the init () method is called only once

(3) Request processing: The servlet container invokes the servlet's service () method to process the request. It is important to note that the init () method must execute successfully before the service () method call

(4) Service termination: The container invokes the instance's destroy () method so that the instance can free the resources it uses

2) Only one object from beginning to end, multithreading accesses the same servlet through the thread pool

The servlet uses multithreading to handle multiple requests at the same time, and the Servelet container maintains a line pool service request.

The thread pool is actually a set of threads waiting to execute code called Worker threads (Workerthread), and the servlet container uses a dispatch thread to manage worker threads (dispatcherthread).

When the container receives a request to access the servlet, the dispatcher thread selects a worker thread from the thread pool, passes the request to the thread, and then executes the servlet's service method by that thread.

When this thread is executing, the container receives another request, and the dispatcher thread selects another worker thread from the pool to serve the new request, and the container is not related to whether the request accesses the same servlet or another servlet.

When the container receives multiple requests to the same servlet at the same time, the service method of the servlet is executed concurrently in multi-threading.

3) How the single-threaded mode of the realistic servlet

<%@ page isThreadSafe=”false”%>

The life cycle of 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.