jQuery in Action, 3rd edition
The moose likes JSP and the fly likes problem with refreshing jsp page (re-submits form) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "problem with refreshing jsp page (re-submits form)" Watch "problem with refreshing jsp page (re-submits form)" New topic

problem with refreshing jsp page (re-submits form)

Jerry Mus

Joined: Aug 16, 2004
Posts: 2
Within a jsp page, I have this <form> that allows a customer to add a product to my db. After submitting the form (action="samepage.jsp"), the product is added to the db. The problem: When I right-click>Refresh (on IE), the same product ('i.e same name') is added to the db (the form is cleared). Whats going on here, and how do I prevent this from happening without disabling Refresh?
I have these header directives.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63862

You can't practically disable refresh, and preventing caching will not help this issue.

You either need to make sure that the form post isn't hanging around (by redirecting rather than forwarding), or have your server-side code detect this condition and deal with it.

The latter is actually a well-known pattern:

1) When your form is generated, create a unique token value -- usually using the system clock time.
2) Place this value in your form as a hidden form element.
3) Also place the value into the session.
4) When the form is submitted, compare the two values. If they match, remove the session token and proceed. If they don't match, or the session token is missing, you know that the form is being "resubmitted" and you can deal with the situation as appropriate to your application.

[Asking smart questions] [About Bear] [Books by Bear]
Jerry Mus

Joined: Aug 16, 2004
Posts: 2
im not that much familiar with sessions . how do i do step 3 and 4? example code please .
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

"Jerry Mouse" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy. User names cannot be obviously fake.

You can change your user name here.

You can also find a good overview of how to work with sessions in Ch. 9 of the online version of Core Servlets and JavaServer Pages.

Thanks! and welcome to the JavaRanch!

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
himanshu patel
Ranch Hand

Joined: Feb 03, 2003
Posts: 205
You should have Primary key in your db either product name or product id.
Put constraints on it so that it won't allow duplicate raw. Alternatively,
before inserting product into db, check first wheter product with same name already exist, if yes do not insert and return to client with valid
error description.

If you want to become a rich, do not work for others but make others to work for you.
I agree. Here's the link: http://aspose.com/file-tools
subject: problem with refreshing jsp page (re-submits form)
It's not a secret anymore!