Sunday, September 23, 2018

Java Servlet lifecycle


INTRODUCTION

A servlet is managed through a well-defined life cycle that defines how it is loaded and instantiated, initialized, handled customer requests and removed from service. This life cycle is expressed in the API by the init, service and destroy methods of the javax.servlet.Servlet interface that all servlets must implement directly or indirectly through the abstract classes GenericServlet or HttpServlet.

Stages of Servlet Lifecycle:


A servlet goes through the following stages in its life.
• Initialize
• Service
• Destroy


Initialize

When the servlet is created for the first time, it is in the start phase. The web server invokes the init () method of the servlet at this stage. It should be noted here that the init () is called only once and is not called for each request. As there is no constructor available in the servlet, this makes it be used for a single boot, as well as the init () method of the applet

Initialize stage has the following characteristics and usage

• Run once when the servlet loads for the first time
• Each customer request is not called
• The two previous points make the ideal location to perform the boot tasks performed in the constructor in a normal class.

Service

The service () method is the servlet mechanism, which actually processes the client's request. In each client request, the server generates a new subprocess and calls the service () method, as shown in the following figure. This makes it more efficient compared to technologies that become a single subprocess to respond to requests.



The following figure shows the version of the service cycle implementation. At the top of the diagram, we assume that the servlet is subclassing in GenericServlet (Remember, GenericServlet is used to build protocol-independent servlets). To provide the desired functionality, the service () method is replaced. The client sends a request to the Web server, a new chain is created to satisfy this request, followed by the call to the service () method. Finally, a response is prepared and sent back to the user according to the request.



The second part of the figure illustrates a situation in which the servlet is made using the HttpServlet class. Now, this servlet can only serve HTTP requests. In these servlets, doGet () and doPost () are replaced to provide the desired behaviors. When a request is sent to the web server, the web server after creating a chain passes this request to the service () method. The Service () method checks the type of HTTP requests (GET, POST) and calls the doGet () or doPost method, depending on how the request is sent. After forming the response by the method doGet () or doPost (), the response is sent back to the service () method that is finally sent to the user by the web server.

Destroy

The web server may decide to remove a previously loaded servlet instance, perhaps because it is explicitly requested by the server administrator, or the servlet container may be shut down or the servlet idle for a long time or the server may be overloaded. Before that, however, call the servlets destroy () method. This makes it a perfect place to release the acquired resources.

The following figure can help to summarize the life cycle of the Servlet




The sever web creates a servlet instance. After the correct creation, the servlet enters the boot phase. Here, the init () method is invoked once. If the Web server fails in the previous two stages, the servlet instance is downloaded from the server. After the start stage, the servlet becomes available to satisfy customer requests and generate responses accordingly. Finally, the servlet is destroyed and downloaded from the web server.




No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...