Pages Navigation Menu

Coding is much easier than you think

Handling double form submissions in Struts2

Handling double form submissions in Struts2

Double form submissions(either by refreshing or by hitting the browser back button) can be irritating and in some cases leads to big problem .Luckly struts2 has a build in functionality that prevents such scenarios :<s:token/> tag and token interceptor. As an example, let’€™s say that we have a form that looks like this:

<s:form action="someAction">
<s:textfield key="key"/>
<s:textfield key="otherKey"/>
<s:textfield key="someOtherKey"/>

** UPDATE: Struts 2 Complete tutorial now available here.
In order to prevent double submissions all we have to do is to add tag in the above form. This tag will add a unique identifier in the form that will be used later from the token interceptor in order to check if the form is submitted for the first time or not. In case the form is not submitted for the first time, interceptor’s result will be “invalid.token”.

<action name="actionClass" class="com.simplecode.actionClass">
<interceptor-ref name="token"/>
<interceptor-ref name="defaultStack"/>
<result name="input">/inputPage.jsp</result>
<result name="invalid.token">/invalidToken.jsp</result>
<result name="success">/successPage.jsp</result>

Note that “invalid.token” result in the above action declaration actually means that in the case of invalid token the user should be redirected in invalidToken.jsp
Recommended Article


About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.


    its block even first time request..
    please reply

  • Syed Maqbul

    i tried adding the interception in my struts.xml and along with its result as invalid.token
    and then added the in my jsp page,
    when i try to access the form for the first time its not allowing me to enter into form instead its directly showing error
    whatever i have defined in the invalid_token.jsp

    plz me help me out

  • Shubhankar

    This will work but after adding

    to struts.xml, Serverside field/action validation is not working..
    Kindly do me help on this.

    • Mohaideen Jamil

      Please mail your code without library to

      • Vamana

        Dear Mohaideen this is vamana me also similar kind of issue even for first time hit also it is throwing error. Please help me.