[Java Web] How does Servlet work?

1. What is Servlet?

Concept : Java Servlet is a program running on a Web server or application server. It acts as an intermediate layer between a request from a Web browser or other HTTP client and a database or application on the HTTP server.
Positioning : Java Servlet is a server-side program (web application) written in Java.
Function : Its main function is to browse and modify data interactively, and generate dynamic Web content.
Understanding : Servlet in a narrow sense refers to an interface implemented by the Java language, and Servlet in a broad sense refers to any class that implements this Servlet interface. Under normal circumstances, we understand Servlet as the latter.

Two, Servlet life cycle

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

1. Servlet is initialized by calling the init () method.
2. Call the service() method to process the client's request.
3. Terminate (end) by calling the destroy() method.

Finally, the servlet is garbage collected by the garbage collector of the JVM.

Three, HttpServlet processes Http requests

The service() method of Servlet is the entry method of the request. HttpServlet implements the service() method. In this entry method, different methods are called according to different Http request methods (such as GET and POST requests). Most applications are used in conjunction with HTTP. This means that you can take advantage of the features provided by HTTP.

The javax.servlet.http package is the second package in the Servlet API, which contains classes and interfaces for writing Servlet applications, and many types overwrite the types in javax.servlet.

The HttpServlet class covers the javax.servlet.GenericServlet class. Servlet and GenericServlet are not specific to any protocol, but HttpServlet is specific to Http protocol, so when using HttpServlet, you need to use HttpServletRequest and HttpServletResponse objects representing Servlet request and Servlet response.

Steps to instantiate Servlet when Tomcat starts

Insert picture description here

First of all, when Tomcat is started, Tomcat will instantiate the Servlet object through some mechanisms:

1. Generate the service path (resource path) + the mapping relationship between the Servlet object;
2. Each time Http request, it will pass the context path + service Path, find the unique Servlet object;
3. Call the service() method of the Servlet object; 4. In the service method,
organize our doXXX method into a template method (design template).

Four, Servlet design details

1. The init method is designed to be called only once.
It is called when the servlet is created for the first time, and is no longer called every subsequent user request. Therefore, it is used for one-time initialization, just like the init method of Applet.
The servlet is created when the user first calls the URL corresponding to the servlet, but it can also mean that the servlet is loaded when the server is started for the first time.
When a user calls a Servlet, a Servlet instance will be created, and each user request will generate a new thread, which will be handed over to the doGet or doPost method when appropriate. The init() method simply creates or loads some data, which will be used for the entire life cycle of the servlet.
The definition of the init method is as follows:
Insert picture description here

2. The service() method is the main method to perform actual tasks.
The Servlet container (ie Web server) calls the service() method to process the request from the client (browser) and writes the formatted response back to the client.
Each time the server receives a servlet request, the server will spawn 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 and other methods when appropriate.
The following are the characteristics of this method: The
Insert picture description here
service() method is called by the container, and the service method calls doGet, doPost, doPut, doDelete and other methods when appropriate. Therefore, you don't need to do any action on the service() method, just rewrite doGet() or doPost() according to the type of request from the client.

3. The destroy() method will only be called once, at the end of the servlet's 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.
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/114402320