forward (request forwarding) and redirect (redirection)

forward and redirect are two ways to jump to the page

One, request forwarding

1. Grammar:
request.getRequestDispatcher(String path).forward(request,response);

"This path must be added /"

2. The execution process

Forward is the internal jump of the server. The data in the two pages are interconnected, and the same request object is used, but the URL address of the first request is still displayed in the address bar. The user does not feel that the page is redirected, and only one request will be sent. .

Forward the request from servlet1 to servlet2, complete part of the function in servlet1, then jump to servlet2, and continue to complete the remaining functions. Request forwarding is the server's internal processing of a request/response and transfers it to another. For, the client In other words, it only knows the A that it requested first, but not the B or even C and D in the middle. The transmitted information will not be lost.

3. Features

(1) The address bar of the browser will not change, and will not change to the target address
(2) The whole process is a request and a response
(3) The data in the request field can only be accessed by the same request
(5) The final response is to the browser The device is determined by the following servlet
(6) can access the resources in the WEB-INF directory

Insert picture description here

Two, redirect

1. Grammar
response.sendRedirect(String url);

2. The execution process

First initiate a request to access a servlet, the server tells the browser that you need to access another request, the server tells the browser the new requested address, the browser sends a new request, the address bar changes, and it changes to the new requested address .

3. Features

(1) The address bar of the browser changes and becomes the target address.
(2) There are two requests and two responses in sendRedirect().
(3) Because the URL redirection is that the two requests do not interfere with each other and are independent of each other, they are not Sharing the data in the request
(4) The final response to the browser is determined by servlet2 (equivalent to copying the target address to the browser address bar and hitting enter)
(5) URL redirection cannot access the WEB-INF directory Resource
(6) Because the request is sent again from the client browser, the previously transmitted information will be lost during the redirection process.
Insert picture description here

Three, application scenarios

1. The login fails, or is intercepted to the login page. As long as the redirect is required after user input, redirection is generally used. Use forward to jump, the address bar remains unchanged, if you refresh the page (request the address bar), a box will pop up, prompting whether to send data repeatedly (if it is a GET request, there will be no prompt, but the data will still be sent)

2. If the data is obtained from the backend, generally request forwarding (because request sharing)

Guess you like

Origin blog.csdn.net/qq_41504815/article/details/114936521