This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes The browser's refresh button cause trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "The browser Watch "The browser New topic

The browser's refresh button cause trouble

li bei

Joined: Jul 22, 2007
Posts: 20
Hi all.
I am in trouble with the refresh button now . It is like this:
I have two jsp page ,one called a.jsp ,it has a form ,and post to b.jsp.
b.jsp handles the request, and insert a record to the database,then forward back to a.jsp.At this time ,the page seems to be a.jsp ,but the address of the browser is still b.jsp .If refresh , there is one more record inserted to the database.
I think if use a servlet or struts' action to do the work which b.jsp does,the problem will appear the same.
If use redirect , I can't pass parameters. I have to display a "inserted done!" at the a.jsp if the record is inserted into the database ,or display "error" on the page and write-back the form data just inputed.

I wonder how to resolve this problem .
Thanks in advance.

I am sorry that my English is poor.If there is something wrong with my words,please tell me .Thank you very much.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 39544
First of all, for forms that change state (like insert into a database), you should use POST, not GET. Then the browsers will at least warn you that you're about to repeat an action, and give you a chance to cancel, if you reload.

The problem is known as the DoubleSubmitProblem, and one of the ways to avoid it is the PostRedirectGet. There was an excellent article about this in the JavaRanch Journal.

Ping & DNS - updated with new look and Ping home screen widget
I agree. Here's the link:
subject: The browser's refresh button cause trouble
Similar Threads
expired problem in jsp (back button/link)
Problem caused by pressing the 'BACK' button on the broswer
send value using response.sendRedirect()
Refresh Problem.
Popup window using Struts