Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Avoid duble submit on a page.  RSS feed

 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I have an jsp page which submits some information to another jsp which store the information, my question is: How do I avoid user to click the submit button again on page one if he uses the back button??
This to prevent the page to store the informations twice
 
ziqiang wu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Zkr:
in the second page, you can set a value in session to guarante information to be saved only one time.
if(session.getAttribute("alreadySubmit1")==null && request.getParameter("submit1")!=null)
{
session.setAttribute("alreadySubmit1", "yes");
save information;
}
hope it work.
ziqiang wu
Java developer
Wellogix, Inc.
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds reasonable, the thing here is that the user still get the submit button on page 1, ( not storing anything though ) and thinking that the new information is being stored, and second:
when the user what to pick a new item that second item wont be stored because of the value stored in the session, I would have to kill the session each time he pick a new item.
I use javascript to redirect user when he is trying to go back with the browser and seems to work.
Thank you anyway ziqiang


Originally posted by ziqiang wu:
hi Zkr:
in the second page, you can set a value in session to guarante information to be saved only one time.
if(session.getAttribute("alreadySubmit1")==null && request.getParameter("submit1")!=null)
{
session.setAttribute("alreadySubmit1", "yes");
save information;
}
hope it work.
ziqiang wu
Java developer
Wellogix, Inc.

 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zkr, this question has been answered in this thread in the HTML and Javascript forum.
Please don't post the same question in multiple furums since it can waste the time of people who are trying to help you out,
ta,
Dave.
 
ziqiang wu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ZKr:
base on your new requirement, I believe there are several way to solve this problem. but I think a good solution should hide any logic problems and always make clents not realize their wrong operation, but always get correct results.
I dont like make any unexpected things happened to clients, for example, if client click back button or something else, then they are kicked out of "current status", and forced to be in a unexpected "status".
one way I think should meet your new requirements not by javascript.
in the second page, assumed that you get information(String) by these method.

//initiate, get value and count how many value you got
int count=0;
String ss1=request.getParameter("information1");
count++;
String ss2=request.getParameter("information2");
count++;
.
.
.
Vector forSave=new Vector(), savedInfo=new Vector();
//save all coming information in vector forSave every time
for(int i=1; i<=count; i++)
forSave.addElement("ss"+i);
//if this is first time, no old value to compare, but need to save these value for comparation next time

if(session.getAttribute("savedInfo")==null && request.getParameter("submit")!=null)
{
save ss1, ss2, ss3....;
session.setAttribute("savedInfo", forSave);
}
//if this is not first time, you need get old saved value to compare with current value. only those value that never be saved before can be saved this time
if(session.getAttribute("savedInfo")!=null && request.getParameter("submit")!=null)
{
savedInfo=(Vector)session.getAttribute("savedInfo");
for(int i=0; i<forSave.size(); i++)
for(int j=0; j<savedInfo.size(); j++)
if((String)forSave.get(i).equals((String)savedInfo.get(j)))
{
forSave.removeElement((String)forSave.get(i));
break;
}
else
if((j+1)==savedInfo.size())
savedInfo.addElement((String)forSave.get(i));
//right now, every string in vector forSave never saved before

if(forSave.size>0)
{
retrive information form vector forSave and save them to database or what ever;
}
//save vector savedInfo in session for comparation next time
session.setAttribute("savedInfo", savedInfo);
}
this code is just showing an algorithm to compare coming infomation with old saved infomation. only those information never saved before can be saved only this time.
hope it is helpful for u
Ziqiang Wu
Java Developer
Wellogix, Inc.
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is not my intention to waste people's time by submiting the same questions in two forums, but have diferent approach in the same problem, because in this case the problem could have been solved using JavaScript or JSP. ( full cliente side or full server side solution )
Sometimes people that are good JavaScript programmers know little about JSP and viceversa JSP programmers know little about javascript programming ( sometimes know little of both like me ) and sometimes are good in both.
I didn't know were my question should be posted in, thus I posted in both forums.
Thanks to ziqiang and to A1ien51 CNEUFOs ( did he change his name already ?? ) for their help.

ziqiang: Your solution could be summarized like this ?
hold all the information to store, put it in the session and compare it with previous stored values ( if any ).
If they are the same, do nothing is already stored, if its different means the user change his mind or pick a new item, so new data must be stored.
This is the idea right ??
I'll try it out.
Happy coding.
Zkr Ryz.
Originally posted by David O'Meara:
Zkr, this question has been answered in this thread in the [b]HTML and Javascript forum.
Please don't post the same question in multiple furums since it can waste the time of people who are trying to help you out,
ta,
Dave.[/B]

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!