File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes repeated insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "repeated insert " Watch "repeated insert " New topic

repeated insert

manu chaox
Ranch Hand

Joined: Apr 10, 2006
Posts: 110
I have a following problem. I have a JSP page which call java function first.


All works fine. But if I refresh page, a row will be inserted in database again.

Can I prevent repeated insert?
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

As a general rule, you should avoid any database operations that change the state of your DB or app on requests of type GET.

With requests of type POST, the browsers will help you a bit by either using a cached version of the page, if available, or by warning the user that refreshing this page (or going to it with the back button) will cause the post that generated it to be repeated.

If it's too late for that, you could try putting a marker in session scope when the user first access this page. Then check for the marker before performing your database insert.

That would, at least limit inserts to one per user session.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: repeated insert
It's not a secret anymore!