The following is a simple example of a
JSP page that uses the Post-Redirect-Get (PRG)
pattern.
It uses scriptlets for backward compatibility and to allow the entire pattern to be written in a single JSP page.
Note: this does not mean that you should use scriptlets in your pages. It is done here for illustration purposes only!
Lines 1 through 16 simulate typical database read/write operations by reading and appending to a
string of text (comments) bound to session scope.
The key to implementing the PRG pattern is to insure that a page is never displayed as a result of a request of type POST. Here, this is done by redirecting to a read-only version of the page after performing a database write (line 16).
To
test, copy this page (without the line numbers) into your application and access it through the browser. Add a comment and then click the refresh button. Note that the previous write operation has not been repeated.
Also see
DoubleSubmitProblem