• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with refreshing jsp page (re-submits form)

 
Jerry Mus
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.


Thanks,
Jerry
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jerry Mus
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im not that much familiar with sessions . how do i do step 3 and 4? example code please .
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic