1

I came across this <s:token /> tag and it is enclosed in a <s:form></s:form> tag in a JSP Page. Some other StackOverflow link discusses its usage in maintaining session and prevention of double submission of forms. What exactly is the use of this tag? If it prevents double submission of form then what happens when I submit form and how does it prevent it?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Paramvir Singh Karwal
  • 597
  • 1
  • 10
  • 24

2 Answers2

4

Here I am answering my own question because this may help someone in future, though I will try to dig more on this and get it updated.

The plain English explanation I found:

"The token tag generates an unique token which is used to find out whether a form has been double submitted. When the form is rendered, a hidden variable is placed as the token value. Let us say, for example that the token is "ABC". When this form is submitted, the Struts Filter checks the token against the token stored in the session. If it matches, it removes the token from the session. Now, if the form is accidentally resubmitted (either by refreshing or by hitting the browser back button), the form will be resubmitted with "ABC" as the token. In this case, the filter checks the token against the token stored in the session again. But because the token "ABC" has been removed from the session, it will not match and the Struts filter will reject the request." (Source)

However these two StackOverflow answers add more to knowledge.

Community
  • 1
  • 1
Paramvir Singh Karwal
  • 597
  • 1
  • 10
  • 24
0

The purpose of <s:token/> tag:

Stop double-submission of forms.

The token tag is used to help with the "double click" submission problem. It is needed if you are using the TokenInterceptor or the TokenSessionInterceptor. The s:token tag merely places a hidden element that contains the unique token.

(Source: https://struts.apache.org/docs/token.html )

What is the use of token tag in Struts2?

See example.

How it works?

It works mainly by using a class, a Helper class and a configuration xml file: Token.java , TokenHelper.java, struts-token.xml.

Vy Do
  • 46,709
  • 59
  • 215
  • 313