Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent form resubmission and insert into database on page refresh

I have a servlet which takes data from the form and inserts it in the database.In servlet I make use of beans and get/set data via beans.Once the insert happens ,user is redirected to the view which shows as how exactly the page looks.This page is JSP. Now when I do this page refresh(jsp),it again does form data insert into the database.Now I need to prevent this from happening.What I was thinking was rather than redirecting it to jsp,let me redirect to another servlet and from that servlet let me do the get request and forward it to jsp for view.With this approach, I have refactor my jsp and I have lot many jsp which will be impacted.

Is their any other way I can prevent insert into the database when page refresh is done ?

I also checked on database side assuming that I can add unique constraints, I do have some unique columns that gets generated via initial servlet,so on each insert values are different,so I cannot really use the solution on DB side.

Can someone suggest me if there exists work arounds for this ?

Update:Adding code which does insert, I am refreshing the page manually.Seems like this is bug which I missed out to consider earlier.It does the insert correctly in the database and shows correctly all data in the view jsp.When the view jsp is refreshed, I get double inserts in the database.

Update .removed the code.Will post relevant parts of the code rather than whole code.

Issue is resolved.

 javax.servlet.http.HttpSession session = request.getSession();            
        if (session.getAttribute("insertflag") == null) {
            mybean sf = new mybean();
            sf.insert(form);
            session.setAttribute("insertflag", insertid);
            RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/test/showtestform.jsp");                
            rd.forward(request, response);
        } else {                
            RequestDispatcher rd = request.getRequestDispatcher("/errorpage.jsp");
            rd.forward(request, response);
        }

In error page jsp I remove the attribute set and then provide the link in that page to come back again to home page.

like image 969
Kiran Badi Avatar asked Jan 07 '13 03:01

Kiran Badi


2 Answers

A simple workaround can be that you put a flag in Session; whenever insert is done.. And before inserting always check session if this flag is present.

In the servlet; Before inserting into the database; make following check:

if (session.getAttribute("recordInsertedSuccessfully") == null )
{
   //proceed with insertion
   //after inserting into the database we should do :
   session.putAttribute("recordInsertedSuccessfully","true");
} else {
   //case of form re-submission
}

If there is a possibility of a valid scenario of user inserting two of these records ( might be user browsing and then again inserting record in same table ; which is a valid scenario ) ; then we need to also add following code before the form is shown to user:

session.removeAttribute("recordInsertedSuccessfully");
like image 173
Deepak Singhal Avatar answered Nov 14 '22 21:11

Deepak Singhal


submit your form using javascript with onclick event

do not submit form using <input type=submit> and use <input type=button>

like image 21
Manish Nagar Avatar answered Nov 14 '22 21:11

Manish Nagar