Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization Token

 
Hideki Mae
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To all:
Can anybody tell me how to use synchronization token? I have the following on an Action and it will always return false.
boolean valid = isTokenValid(httpServletRequest);
if(valid){
resetToken(httpServletRequest);
System.out.println("The token is valid");
}else{
System.out.println("The token is invalid");
}
This is to prevent resubmission of the page.
I thought you simply check if the token is valid. If it is, you would reset the token. If not, than you would handle the error.
Do I need to add anything to my web page?
Is there a specific link that explains synchronization token? The places I came across were bits and pieces of information and not too specific.

Thank you in advance.
 
Lucian Ochian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look here: http://www.coderanch.com/t/51602/Struts/Duplicate-form-submission-Synchronizer-Token

It works like a dream, thanks to Ravindra Janapareddy that explained this pattern very well and simple.

So you only thing that have to add is to save the token in the loading Action.

Lucian
 
Hideki Mae
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying. I tried it on one of my web pages. It seems to alternate between valid and invalid when I execute the action. What I have are two actions. The first action is for the initial load of the page (similar to DisplayAction in the example). The 2nd Action executes a query on the database and sets up the data to be displayed (similar to SubmitAction). The web page has one button which will execute the 2nd Action each time I press a button. When I press it the first time the data gets loaded and isTokenValid() will return true. If I press the button again, (basically I am executing another query) it will become invalid. And the validation alternates each time I execute the 2nd action (press the button). Am I doing something wrong?
Thanks for any information you can give me.
 
Hideki Mae
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out that all I needed to do was add saveToken() after the resetToken() statement. It is working now.
Thank you for all your help.
 
Lucian Ochian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful!
Take a look again to the link that I gave you because I added a post reply there!

Regards,
Lucian
 
Hideki Mae
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. It helped alot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic