Preface:
uses the most understandable examples and humorous pictures to lead you to appreciate the beauty of the language of java
When everyone is learning javaweb, there is a very important knowledge point, which is to realize our sharing through forwarding and redirection Information and page jumps. Then why do we need to jump and share data?
- jump: separation of responsibilities is required (Servlet is good at writing code and processing business logic, jsp is good at displaying pages) and jumps to the corresponding display interface through the logic processing of the code in the background. For example: click the login button, and jump to the login success or failure interface after background code verification;
- sharing: Http is stateless, and data cannot be transferred when we jump, so data sharing is required;
Let’s look at forwarding first And how redirection is applied in the code
forwarding method:
req.getRequestDispatcher("Path to jump").forward(request, response);
redirection jumping method:
resp.sendRedirect("To jump Transfer path");
The process of request forwarding (RequestDispatcher):
The client first sends a request to the server. The server finds the matching servlet and specifies it to execute. When the servlet is executed, it calls getRequestDispacther( ) method, forward the request to the specified jsp page, and the entire process is completed on the server side.
is completed in the same request, so the servlet and jsp share the same request. Everything placed in the servlet can be taken out in the jsp. Therefore, the jsp can get the result getAttribute(), getAttribute( ) comes out and returns the result to the client after execution. The whole process is a request and a response. The working principle of
redirection (sendRedirect):
The client sends a request to the server, and the server matches the servlet. This is the same as request forwarding. After the servlet is processed, the sendRedirect() method is called. This method is the response method, so, When the servlet is processed and sees the response.senRedirect() method, it immediately returns the response to the client. The response line tells the client that you must send another request to access the jsp page.
Immediately after the client receives this request, it immediately sends a new request to request the jsp page. The two requests here do not interfere with each other and are independent of each other. Anything setAttribute() in the previous request will be in the subsequent request. Neither can be obtained. It can be seen that there are two requests and two responses in sendRedirect(). Let’s use another picture to quickly understand forwarding and redirection Everyone knows that the difference between forwarding and redirection is the number of different requests, whether the address in the address bar has changed, whether the resources and capabilities under web-inf can be accessed No access to external resources. The most important point is whether data can be shared. Many people have expressed doubts about how forwarding achieves data sharing or why redirection does not support data sharing. Let’s analyze the picture with examples I asked the squad leader to borrow 15 yuan. The squad leader said he didn’t have it (actually he didn’t want to lend it to me). The squad leader said that Xiao Wang had it and asked me to borrow money from Xiao Wang. I had no choice but to go to Xiao Wang to borrow money. Xiao Wang was very generous and directly lent me 100. If it was not enough, I would go to him again. In fact, this is the redirection we are talking about here. As you can see, we sent two requests, one to borrow money from the squad leader and one to borrow money from Xiao Wang. The difference in forwarding is that I went to the squad leader to borrow money. Although the squad leader had no money, the squad leader said I would lend you money. The squad leader went to Xiao Wang to borrow money and gave me the borrowed money. Relatively speaking, I only sent one request, which was to the squad leader, and the squad leader forwarded the request, but both the squad leader and Xiao Wang were included in this request. So what is data sharing? As you can see, I sent a request data of 15 yuan to the squad leader, and the squad leader forwarded the request data of 15 yuan to Xiao Wang. When I finally paid back the money, I returned the money directly to the squad leader, who then sent the 100 yuan to Xiao Wang, instead of me returning it directly to Xiao Wang. The squad leader is equivalent to the middleman, and his role is data sharing and interaction. Why can't data sharing be achieved through redirection? First of all, I sent a loan request to the squad leader. The squad leader said no. The request ended here and no data sharing occurred. The borrowing of money from Xiao Wang and I was just a simple data interaction, not a sharing of data because there was nothing with the squad leader. relation. This is my summary of redirection and forwarding content, I hope it can help everyone understand.