The principle of response.setStatus

when springMvc processes HttpRepsonse, set response.setStatus (301) and response.sendRedirect ("/ hello") at the same time;, HTTP response Headers status is 302 when

it"s the same with changing the positions of the two, so I"d like to understand the specific principle

.

related codes

// output
Request URL: http://localhost:9999/account/list
Request Method: GET
Status Code: 302 
Remote Address: [::1]:9999
Referrer Policy: no-referrer-when-downgrade
HTTP/1.1 302
Location: http://localhost:9999/hello
Content-Length: 0
Date: Sun, 29 Jul 2018 17:38:31 GMT
Mar.31,2021

has not used springMVC, is it estimated that this 302 is sent in sendRedirect? take a look at the source code of this function, you will know


you take a look at the names sendRedirect and setStatus . Send is sent, and sendRedirect will send the response directly to the user, so it is useless for you to set the status code of the response later, and the response has been sent back to the client.


according to the problem description, the implementation in sendRedirect should look like this

function sendRedirect(url) {
    this.statusCode = 302;
    this.setHeader('Location', url);
}

and the implementation of setStatus is like this

function setStatus(code) {
    if(!this.statusCode) {
        this.statusCode = code;
    }
}
Menu