1

I have a simple html page with the purpose of performing changes to an entity which is written to the database. The page has two forms:

  • GET: select the entity to change
  • POST: process the changes to the entity and write it to the database

When the POST action is performed I want to display the same page again, in addition I want to keep the entity parameter as set by the GET action. Currently I am doing the following at the end of the doPost method:

response.sendRedirect(path + "?entity=" + entityValue);

Which works perfectly fine, but after reading the differences between forward and sendRedirect I thought I have to use forward, but that won't work since the doGet method will not be executed as with sendRedirect. So is my approach correct or should this solved in another way?

Community
  • 1
  • 1
Mahoni
  • 7,088
  • 17
  • 58
  • 115
  • Why exactly did you thought that you have to use a forward? The top answer in the linked question already recommends to use redirect after a successful POST. – BalusC May 22 '12 at 18:42

1 Answers1

8

Your approach is excellent. It uses the post-redirect-get pattern, which has several advantages:

  • clean URL that is bookmarkable and can be sent by email for example
  • no risk of resubmitting the modification by hitting the Refresh button
  • clean navigation through the browser history
  • separation of responsibilities between the get and the post URLs

So keep everything as it is. A forward is a less optimal solution than a redirect in this case.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255