Preventing Duplicate Form Submission in Struts using TOKEN

Duplicate form submission may occur in the following situations:-
  • Using Refresh button
  • Using the browser back button to traverse back and resubmit form
  • Using Browser history feature and re-submit form.
  • Malicious submissions to adversely impact the server or personal gains
  • Clicking more than once on a transaction that take longer than usual
What happens in form submission? Suppose you have a form named adduser.jsp and an Action named saveUser.do. So, on clicking submit in the adduser.jsp page struts finds the mapping saveUser.do and redirect to this url. So, in the browser url you will find /app/saveUser.do.
and when you click on refresh same url is called and same form is submitted again. Struts provides several ways to protect this.
  • HTTP redirect after form submission
  • using TOKEN
HTTP redirect

Suppose form submission result is shown in a page called success.jsp. So, if redirect is set true in forward of action in struts.config URL in the URL bar becomes /app/success.jsp. So, if you refresh on again the success.jsp is called not the /app/saveUser.do. So, in this way we can protect against refresh. But, unfortunately this does not protect against goint to back button of browser and submit the form again.

Using TOKEN

This method generates a Token using session id, current time and messagediagest and save this token to session. The action responsible for rendering the form save the token using saveToken method. So, the session is saved in session and a hidden field name token with the value is set to html form. If you view the source of html form page you will find the hidden form as follows

<input type="hidden" name="org.apache.struts.taglib.html.TOKEN"value="some value">
When client submit the form this hidden field also submitted. Struts check the two TOKEN (token from session and token submitted). We can check the two token using isTokenValid method. If they are equal this method returns true and the form is submitted for the first time. If not equal then duplicate form submission. Checking code is shown below:-


if(isTokenValid(request))
resetToken(request);
else{
request.setAttribute("brtcerror","Duplicate Form Submission Error");
return mapping.findForward("error");
}


resetToken method is used to clear the Token set for this action. Because if you have multiple form you must clear the Token. Other wise for new form you will always find duplicate form submission error. Suppose action addUser renders adduser.jsp page. So, you have to save token in addUser corresponding class by calling saveToken(request) method. You should validate this token in the submit action which is saveUser.do here.

Comments

Moshahid said…
Notun post dau.
Niomito diba.
gdgdfgfgfg said…
HI Mohammad,
Thank you for posting such nice article. Can you please send me an example of duplicate form submission on suresh.ghuge@gmail.com.

Many thanx in adv.
Unknown said…
please send me an example. if possible at ram.paluvai@gmail.com
Unknown said…
đồng tâm
game mu
cho thuê nhà trọ
cho thuê phòng trọ
nhac san cuc manh
số điện thoại tư vấn pháp luật miễn phí
văn phòng luật
tổng đài tư vấn pháp luật
dịch vụ thành lập công ty trọn gói

một giấc không tỉnh như vậy.

Đến cuối cùng rốt cuộc đột phá, cũng tiến vào một cái cảnh giới mới!

Kiếm linh sở dĩ nói đây là một cái cảnh giới mới, chính là bời vì, cảnh giới này, liền ngay cả kiếm linh ở sinh mệnh vô hạn cũng là chưa từng có nhìn thấy qua, chưa từng có lĩnh ngộ qua.

Cảnh giới không nhất định là đỉnh phong cao thâm đến không thể trèo lên, nhưng tuyệt đối là chưa từng có ai! Một loại cảnh giới từ xưa đến nay, chưa từng có người tiến vào!

Công lực của Sở Dương cũng không có tăng lên bao nhiêu, nhưng tĩnh thân cảnh giới của hắn, lại là đột phá đến một loại tầng thứ cho đù là cường giả kiếm hoàng cũng không nhất định có thế đủ đạt tới!

Kiếm linh vui mừng cười, bây giờ Sở Dương đã đột phá phạm trù cửu kiếp kiếm, hắn tuy rằng vẫn làm luyện Cửu kiếp kiếm công pháp, đi vẫn là con đường kia. Nhưng hắn đã có thể tuy thời siêu thoát ra con đường này...

Nói cách khác, ngay cả Cửu kiếp kiếm thần dị nữa cũng đã không thể khống chế một vị Cửu kiếp kiếm chủ bây giờ này.

Cửu kiếp kiếm chủ, Cửu kiếp kiếm ở trước, chủ ở phía sau.

Nhưng bay giờ Sở Dương, lại đã thật thành chủ nhân của Cửu kiếp kiếm!

Đây là hai khái niệm hoàn toàn khác nhau!

Sở Dương liên tục luyện kiếm, thậm chí có đôi khi ăn uống ngay tại đáy nước. Tùy tiện bắt lấy một con cá ăn liền, Hắn đang không biết mệt mỏi, và quen thuộc loại cảnh giới này, quen thuộc loại cảm thụ mới lạ này!

Popular posts from this blog

Oracle Export, Import using sqlplus

Custom Request Processor in Struts