Home > Common Problem > What is the life cycle of servlet

What is the life cycle of servlet

爱喝马黛茶的安东尼
Release: 2020-01-07 13:18:02
Original
4360 people have browsed it

What is the life cycle of servlet

Servlet life cycle: first load the servlet class, instantiate the servlet, then initialize the servlet and call the init() method, then call the service method of the service to process the doGet and doPost methods, and finally The destroy method is called when the container is closed.

The Servlet life cycle can be defined as the entire process from creation to destruction. The following is the process that a Servlet follows:

·A Servlet is initialized by calling the init () method.

·Servlet calls the service() method to handle the client's request.

·Servlet is terminated (ended) by calling the destroy() method.

·Finally, Servlet is garbage collected by the JVM's garbage collector.

Now let us discuss the life cycle methods in detail.

init() method

The init method is designed to be called only once. It is called when the Servlet is first created and is no longer called on each subsequent user request. Therefore, it is used for one-time initialization, just like the Applet's init method.

A Servlet is created when the user first calls the URL corresponding to the Servlet, but you can also specify that the Servlet is loaded when the server first starts.

When a user calls a Servlet, a Servlet instance will be created. Each user request will generate a new thread and hand it over to the doGet or doPost method when appropriate. The init() method simply creates or loads some data that will be used throughout the Servlet's lifecycle.

The init method is defined as follows:

public void init() throws ServletException {
  // 初始化代码...
}
Copy after login

service() method

service() method performs the actual task Main method. The Servlet container (that is, the Web server) calls the service() method to handle the request from the client (browser) and writes the formatted response back to the client.

Every time the server receives a Servlet request, the server will create a new thread and call the service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods when appropriate.

The following are the characteristics of this method:

public void service(ServletRequest request, 
             ServletResponse response) 
    throws ServletException, IOException{
}
Copy after login

The service() method is called by the container, and the service method calls doGet, doPost, doPut, doDelete and other methods at appropriate times. So, you don't have to do anything with the service() method, you just need to override doGet() or doPost() depending on the type of request from the client.

The doGet() and doPost() methods are the most commonly used methods in every service request. Below are the characteristics of both methods.

doGet() method

The GET request comes from a normal request to a URL, or from an HTML form without specifying METHOD. Handled by the doGet() method.

public void doGet(HttpServletRequest request,
            HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet 代码
}
Copy after login

doPost() method

The POST request comes from an HTML form that specifically specifies METHOD as POST, which is determined by the doPost() method deal with.

public void doPost(HttpServletRequest request,
             HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet 代码
}
Copy after login

destroy() method

destroy() method will only be called once, at the end of the Servlet life cycle. The destroy() method allows your 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 calling the destroy() method, the servlet object is marked for garbage collection. The destroy method is defined as follows:

public void destroy() {
    // 终止化代码...
}
Copy after login

Architecture diagram

The following figure shows a typical Servlet life cycle scheme.

·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.

·Then the Servlet container handles multiple requests generated by multiple threads, each thread executing the service() method of a single Servlet instance.

For more FAQ, please visit the PHP Chinese website.

The above is the detailed content of What is the life cycle of servlet. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template