• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

problem with refreshing jsp page (re-submits form)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im not that much familiar with sessions . how do i do step 3 and 4? example code please .
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
a wee bit from the empire
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic