Understanding of HttpServletRequest

The web server receives an http request and creates an HttpServletRequest and HttpServletResponse object for each request

从客户端取数据找HttpServletRequest
向客户端发送数据找HttpServletResponse

1. The running process of Servlet

​ For users, a request is initiated on the client (for example, to query a certain type of product), and the results are displayed on the page (for example, n products are queried, and the content of the first page is displayed on the page). It’s just one click of the left mouse button, but on the server, it’s much more than just one click. Let’s take a look at what the server has done.

​ First, let's look at the following picture:

From the figure, we can see that,

The client's network request will first be received by the Http server (also called a web server, web container, which needs to provide the environment required for the web application to run, and receive the client's Http request);

The web server transfers the request to the corresponding Servlet container according to the requested path (also known as the Servlet engine, which provides environmental support for the operation of the Servlet, which can be understood as tomcat or other servers);

The Servlet container loads the Servlet according to the corresponding virtual path (configured in @WebServlet), and creates an instance of the Servlet (calling the init method) if the Serlvet has not been instantiated;

The Servlet container creates a ServletRequest object (in which the HTTP request information is encapsulated) and a ServletResponse object that can respond to the HTTP request according to the user's HTTP request.

Then call the rewritten service(ServletRequest req, ServletResponse res) method in HttpServlet, and in this method, transform the two objects ServletRequest and ServletResponse downwards, and get the two objects HttpServletRequest and HttpServletResponse that we are very familiar with.

Then forward the client's request to the protected modified service in HttpServlet (HttpServletRequest req, HttpServletResponse resp);

service(HttpServletRequest req, HttpServletResponse resp) calls different methods according to the requested method (get, post, put, delete, head, options, trace), such as doGet, doPost;

After the server finishes processing the Http request, it returns the processing result to the client as an Http response according to the HttpServletResponse object.

​ The above is the operation performed by the server before a client initiates a request and receives a response. For a Servlet, its execution process is equivalent to its life cycle: 1. Loaded by the Servlet container ------> 2. Receive the Http request from the client forwarded by the servlet container --------> 3. After processing, return the processing result to the client ------> 4. The web service is destroyed when it terminates. Among them, steps 2 and 3 may be executed multiple times due to multiple requests from the client during the running of the web service, and steps 1 and 4 may also be executed multiple times due to service restart or active destruction.

Guess you like

Origin blog.csdn.net/m0_59281987/article/details/129658168