skwisgaar
skwisgaar

Reputation: 987

HttpServletResponse.sendRedirect() change method type

just a short question - what method (e. g. GET, POST etc.) does sendRedirect use? Does it inherits it from request? And, if so, is it possible to change it to another? Thanks!

Upvotes: 2

Views: 2804

Answers (2)

ZhongYu
ZhongYu

Reputation: 19712

This matter is a little messy, see spec https://www.rfc-editor.org/rfc/rfc7231#section-6.4

There are a bunch of redirect response codes - 300 301 302 303 307 308. We can probably summarize the rules as the following:

  1. by default, redirect does not change request method
  2. 303 always changes the request method to GET (unless it's HEAD)
  3. 301/302 changes POST to GET

to put it in a table ( "-" means the same method )

       300   301   302   303   307   308  
 HEAD   -     -     -     -     -     -
  GET   -     -     -     -     -     -
 POST   -    GET   GET   GET    -     -
other   -     -     -    GET    -     -

Browsers usually won't auto follow a redirect to POST.

In practice, 303 is the most often used code for web applications.

Upvotes: 2

folkol
folkol

Reputation: 4883

sendRedirect sends a RESPONSE, not a REQUEST. And hence, has no METHOD.

It is up to the client, but it will normally do a GET or a HEAD request to the Location that you provide on the redirect.

Upvotes: 5

Related Questions