response.sendRedirect calls are very commonly used in servlets. They are used extensively to implement PRG patterns to avoid duplicate form submissions by page refresh and to be more bookmark-friendly. I have had requirements in my application where I need to redirect the request with another POST call. response.sendRedirect would not work in this scenario and I have often been forced to render a blank page that would post itself as soon as it is loaded.
I was curious to know if there is a more elegant way to do this and checked up HTTP 1.1 specification. To my surprise, 302 status code which is predominantly used for response.sendRedirect requests does not explicitly state that the second request be made over GET. The specification seems to suggest that the original method should be used for the second request as well. But no http client that I know of does this. 302 response for a POST request is always followed by a GET request to the new resource. And, there is another status code 303 to always do a GET for the second call. In other words, according to specifications, 302 is expected to work the way I desired but no modern browsers support it. Having said that, the specification acknowledges this fact and also doesn’t unambiguously state that the same method be used for the second call after a 302 response. You can read the complete text here.
I did some more research on this and came across this post, which confirmed by findings.
- To have a guaranteed POST-REDIRECT-GET behavior, server should respond with a 303, NOT 302.
- Ideally, POST-REDIRECT-POST should be possible with a 302, possibly with a warning to end user. But in practice, this behavior is not supported by any of the popular browsers.