[Javaweb] Servlet working principle and life cycle


Java Servlet technology, referred to as Servlet technology, is the underlying technology for developing Web applications in Java.

1. Keywords

  • Servlet
    Servlet is a type of JavaEE specification, mainly to extend the functionality of Java as a Web service. In order to facilitate third parties to comply with this specification, Sun (now Oracle) provides a series of related interfaces, namely Servlet API.
  • Servlet application is
    a Java program that directly or indirectly implements the Servlet interface and needs to run in a Servlet container. It is mainly used to generate dynamic Web pages. Servlet applications cannot run independently and must be deployed into a Servlet container.
  • Servlet Container
    A Servlet container (Servlet engine) is a part of a web server or application server that is used to provide network services on top of sent requests and responses, decode MIME-based requests, format MIME-based responses, i.e. the Servlet container is used to Receive the client request, process the protocol, request content, etc., initialize the Servlet instance (only the first initialization is required) and call the corresponding method of the Servlet application, and then the Servlet application returns the processing result, and then returns it to the user client through the Servlet container.
  • Tomcat container
    Tomcat container, also called application server, is also called Servlet container. In fact, in essence, the Tomcat container has the function of a Servlet container and is an open source implementation of the Servlet container, but it is not just a Servlet container.

Insert image description here

For the relevant content of keywords, please refer to the CSDN blogger "姠濢臇者", click to view the original text


2. Working principle

  • The Servlet interface defines the contract between Servlet and servlet container: the Servlet container loads the Servlet class into memory, generates Servlet instances and calls specific methods [
    tips : In an application, there can only be one instance of each Servlet type]

  • The user makes a request, and the Servlet container calls the Servlet's Service() method, passing in the ServletRequest object and the ServletResponse object
    [ tips : The ServletRequest object and the ServletResponse object have been encapsulated by the Servlet container (such as TomCat), and programmers can use these two objects directly. 】

  • ServletRequest encapsulates the current Http request. ServletResponse represents the current user's Http response. Programmers only need to directly operate the ServletResponse object to easily send the response back to the user.

  • For each application, the Servlet container creates a ServletContext object, which encapsulates the environment details of the context (application) [tips: There is only one
    ServletContext for each application, and there is only one ServletContext for each Servlet object that encapsulates the Servlet configuration. ServletConfig object]


3. Life cycle

Servlet is a singleton

Servlet life cycle can be defined as the entire process from creation to destruction

  • Instantiation – Create a servlet instance
  • Initialization – init()
    is only called once, when the Servlet is first created, and will no longer be called on each subsequent user request.
  • Processing requests – service()
    is the main method to perform actual tasks. The Servlet container (i.e., Web server) calls the service() method to process requests from the client (browser) and writes the formatted response back to the client.
  • Service termination – destroy()
    is only called once. Called at the end of the Servlet life cycle,
    it can cause the Servlet to close the database connection, stop the background thread, write the cookie list or click counter to disk, and perform other similar cleanup activities. After the call, the servlet object is marked for garbage collection
  • GC garbage collection (can be regarded as part of service termination)

As picture description:
Insert image description here

Specific process:
Insert image description here


4. Architecture diagram

The first HTTP request that reaches the server is delegated to the Servlet container

The Servlet container loads the Servlet before calling the service() method

The Servlet container handles multiple requests generated by multiple threads, and each thread executes the service() method of a single Servlet instance.

Insert image description here

Guess you like

Origin blog.csdn.net/m0_50609545/article/details/120295027