Home >Common Problem >How to understand the life cycle of servlet

How to understand the life cycle of servlet

coldplay.xixi
coldplay.xixiOriginal
2020-11-02 11:52:2037258browse

Servlet life cycle: 1. In the initialization phase, the Servlet container will create a Servlet instance and call the [init()] method; 2. In the client request processing phase, each time a client request is received, the server will Generate a new thread for processing; 3. In the termination phase, call the destroy method to terminate.

How to understand the life cycle of servlet

Servlet life cycle:

1. servlet life cycle

There are three main methods:

  • init() initialization phase

  • ##service() processing client request phase

  • destroy() termination phase

Initialization phase:

The Servlet container loads the Servlet. After the loading is completed, the Servlet container will Create a Servlet instance and call the init() method. The init() method will only be called once.

The Servlet container will load the Servlet in the following situations:

The Servlet container will automatically load some when it starts. servlet, to achieve this you need to add 1 to the web.

##Processing client request phase:

Every time a client request is received, the server will generate a new thread to process it.

For the user's Servlet request, the Servlet container will create a request-specific ServletRequest and ServletResponse.

For tomcat, it will put the passed parameters into a HashTable, which is a key-value mapping of String–>String[]

Termination phase:

When the web application is terminated, or the Servlet container terminates, or the Servlet reloads a new Servlet instance, the Servlet container will call the Servlet's destroy() method

2 , The working principle of servlet

The client sends a request, and the Servlet calls the service() method to respond to the request. The service() method will match the requested method, enter the corresponding logic layer, and complete the request. the response to.

But there are no doGet(), doPost() and other methods in the Servlet interface and GenericServlet interface. These are defined in HttpServlet, but all Error information is returned, so these methods must be rewritten every time a Servlet is defined.

Sertvlet and GenericServlet are not specific to any protocol, while HttpServlet is specific to the Http protocol, so the service() method in HttpServlet forces ServletRequest and ServletResponse into HttpRequest and HttpResponse, and finally calls its own service method to complete the response.

Related free recommendations:

Programming video courses

The above is the detailed content of How to understand the life cycle of servlet. 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