Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Struts2 Issue - Multiple Form Submits due to Browser Refresh

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a UI page where we enter two values subject and message and click on Add Message and the form gets submitted. The user is clicking on browser refresh continuously which is causing multiple form submits and the same message gets displayed again and again. Please provide pointers as to how can I resolve this.

Appreciate your help.

Thanks,
Swapnika.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Add message" is a post action?

Have you tried redirecting the post to a get? The redirect will return a "get" to the user, so they can refresh as many times as they like and it doesn't resubmit.... just re-get.

Google: get redirect post pattern
 
Swapnika Mishra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, I forgot to add in my post, I tried using post redirect get but the jsp is not getting rendered correctly. So I tried with the below approach by setting a use once only token in session and comparing it with request. However when I am trying to reset the session token, it is not getting updated. Can you please let me know what is wrong in the below code.

Action Class
//Set Token which renders the jsp page
getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "123");

This is in my jsp
<s:hidden name="token" value="123"/>

Action class
String requestToken = getToken();
String sessionToken = (String)
getRequest().getSession().getAttribute(Globals.TRANSACTION_TOKEN_KEY);

if(requestToken.equalsIgnoreCase(sessionToken)){
//reset the token
getRequest().getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "124"); -> This is where the session value is not getting updated to 124 when I press F5.

//perform update

}

Please assist.

Thanks,
Swapnika.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using Struts 2 (if you aren't, you should be)? If so, this kind of thing is built in.
 
Swapnika Mishra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried using TokenInterceptor but it doesnt stop the form from multiple submissions when the user does a browser refresh by clicking on F5. Also, I want the user to stay on the same page but the form should not be submitted multiple times on browser refresh.

I tried using Post Redirect Get but I am not able to figure out why the page is not rendered properly. Please let me know if there is something i did incorrect over here.

My original code was

<action name="update-test" class="testAction" method="test">
<result name="success">/test/xyz/test-result.jsp</result>
</action>

<action name="update-test" class="testAction" method="test">
<result name="success" type="redirectAction">forward-test</result>
</action>
<action name="forward-test" class="testAction" method="forward">
<result name="success">/test/xyz/test-result.jsp</result>
</action>


Apr 26, 2014 4:12:02 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.portal.tiles.tickets.tests_002test_002dresult_002dv1_jsp._jspService(tests_002dtest_002dresult_002dv1_jsp.java:196)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)

When I did a view source on Line 196, I see the below. Please help.

<div id="tabXYZ" class="panel">
<img src="/images/arrow_mainmenu.jpg" width="25" height="31" alt="" class="floatingleft">
Browser-refresh.png
[Thumbnail for Browser-refresh.png]
 
Swapnika Mishra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting the following error from browser that "To display this page, Firrfox will repeat any action that was performed earlier" when the user clicks on F5 and the form gets submitted again. I tried using POST REDIRECT GET but facing issues.Please assist.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swapnika Mishra wrote:
When I did a view source on Line 196, I see the below. Please help.



JSP's get compiled into a Servlet. The line number above refers to the line number in the servlet, not the JSP (note the file name). You may be able to recover the generated source to narrow down the issue.

I tried using TokenInterceptor but it doesnt stop the form from multiple submissions when the user does a browser refresh by clicking on F5.



Odd. It works for me. I took the source of the example I wrote for the Indexed Property FAQ entry, entered a line item, then hit F5. An identical line item is added. Then I added the <s:token/> tag to my JSP and added the TokenInterceptor to my interceptor stack and repeated the test. I got an error that there is "no result defined for action action.indexprops.OrderAction and result invalid.token", which is correct behavior for the the token tag, at least as far as I'd set it up.

I am getting the following error from browser that "To display this page, Firrfox will repeat any action that was performed earlier" when the user clicks on F5 and the form gets submitted again



This is correct behavior for Firefox when resubmitting a form.
reply
    Bookmark Topic Watch Topic
  • New Topic