Servlet lifecycle


Release date:2023-12-12 Update date:2023-12-12 Editor:admin View counts:95

Label:

Servlet lifecycle

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

  • Called after Servlet initialization init() method.

  • Servlet call service() method to process the client’s request.

  • Called before Servlet is destroyed destroy() method.

  • Finally, Servlet is garbage collected by JVM’s garbage collector.

Now let’s discuss the lifecycle approach in detail.

init() method

init method is designed to be called only once. It is called the first time the Servlet is created, and not again each time the user requests it. Therefore, it is used for one-time initialization, just like Applet’s init in the same way.

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

When a user invokes a Servlet, an instance of Servlet is created, and each user request generates a new thread that is handed over to the doGet or doPost method when appropriate. The ``init () ``method simply creates or loads some data that will be used for the entire life cycle of the Servlet.

The init method is defined as follows:

public void init() throws ServletException {
  // setup code...
}

service() method

service() method is the main way to perform actual tasks. The Servlet container (that is, Web server) calls service() method to process the request from the client (browser) and write the formatted response back to the client.

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

The following are the characteristics of this method:

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

service() method is called by the container, and the service method calls doGet, doPost, doPut, doDelete, and so on, when appropriate. So, you don’t have to be right. service() method, you only need to override it based on the type of request from the client doGet() or doPost() that’s it.

doGet() and doPost() method is the most commonly used method in each service request. Here are the characteristics of these two methods.

doGet() method

The GET request comes from a normal request from a URL, or from an HTML formthat does not specify a METHOD, which is created by the doGet() methods to deal with.

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet code
}

doPost() method

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

public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet code
}

destroy() method

The destroy() method is called only once, at the end of the Servlet lifecycle. destroy() method allows your Servlet to close database connections, stop background threads, write Cookie lists or click counters to disk, and perform other similar cleanup activities.

In the call destroy() method, the servlet object is marked for garbage collection. The destroy method definition is as follows:

public void destroy() {
  // Terminate Code...
}

Architecture diagram

The following figure shows a typical Servlet lifecycle scenario.

  • The first HTTP request to arrive at the server is delegated to the Servlet container.

  • The Servlet container is calling the `service() load Servlet before themethod.

  • The Servlet container then processes multiple requests generated by multiplethreads, each of which executes a single instance of Servlet ``service()``method.

Image0

Powered by TorCMS (https://github.com/bukun/TorCMS).