title: Prevent form resubmission and insert into database on page refresh
body: <p>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.</p>
  
  <p>Is their any other way I can prevent insert into the database when page refresh is done ? </p>
  
  <p>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.</p>
  
  <p>Can someone suggest me if there exists work arounds for this ?</p>
  
  <p>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.</p>
  
  <p>Update .removed the code.Will post relevant parts of the code rather than whole code.</p>
  
  <p>Issue is resolved.</p>
  
  <pre><code> 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);
          }
  </code></pre>
  
  <p>In error page jsp I remove the attribute set and then provide the link in that page to come back again to home page.</p>