search
HomeCommon ProblemWhat is the life cycle of servlet

What is the life cycle of servlet

Jan 07, 2020 pm 01:18 PM
servletlife cycle

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 {
  // 初始化代码...
}

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{
}

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 代码
}

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 代码
}

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() {
    // 终止化代码...
}

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.