Fórumok

Duplicate Submission of Form

thumbnail
devi nimmagadda, módosítva 12 év-val korábban

Duplicate Submission of Form

Regular Member Bejegyzések: 109 Csatlakozás dátuma: 2010.12.09. Legújabb bejegyzések
Hi,


can anybody know this....

How to prevent duplicate submission of a form ?

Because I am submitting a form with data. When I click on submit button the row is inserted into the database table.

When I refresh the page or go to back button of the same page, The row is inserted again.

Can anybody give suggestion to this.....

Multiple rows are inserting in my table.

Thanks in advance
Hon Hwang, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

New Member Bejegyzések: 17 Csatlakozás dátuma: 2011.07.04. Legújabb bejegyzések
I had similar problem and found a solution. If you are using POST to submit your form, found that this is HTTP POST double submit problem.

Solution is to send a redirect after your action method.
Roshan Qureshi, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Regular Member Bejegyzések: 159 Csatlakozás dátuma: 2010.08.24. Legújabb bejegyzések
This problem is related to any jsp or servlets. Whenever you are submitting any form after that you come back to that page again.
To prevent duplication you should use redirect method instead of using forward method.
This problem is also called as "refresh problem".

Thanks,
Roshan Qureshi
thumbnail
Ketan Savaliya, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Regular Member Bejegyzések: 117 Csatlakozás dátuma: 2011.03.03. Legújabb bejegyzések
devi nimmagadda:
Hi,


can anybody know this....

How to prevent duplicate submission of a form ?



Hi, devi nimmagadda
Try this,
1) get PortletURL from actionrequest in action method

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
String portletName = (String)actionRequest.getAttribute(WebKeys.PORTLET_ID);
PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest), portletName,
themeDisplay.getLayout().getPlid(), PortletRequest.RENDER_PHASE);

2) if u want to set parameter then set to PortletURL

redirectURL.setParameter("view", "abc");

3) at last redirect by actionResponse

actionResponse.sendRedirect(redirectURL.toString());

Hope so it will help u. emoticon
Thanks,
Ketan
thumbnail
KK rajput, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Expert Bejegyzések: 266 Csatlakozás dátuma: 2008.04.10. Legújabb bejegyzések
devi nimmagadda:
Hi,


can anybody know this....

How to prevent duplicate submission of a form ?

Because I am submitting a form with data. When I click on submit button the row is inserted into the database table.

When I refresh the page or go to back button of the same page, The row is inserted again.

Can anybody give suggestion to this.....

Multiple rows are inserting in my table.

Thanks in advance

check below link, it might be useful. If you are having portal source then you can see test redirect mapping in struts-config.
http://kamalkantrajput.blogspot.com/2011/04/struts-tips.html
Henal Saraiya, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

New Member Bejegyzések: 18 Csatlakozás dátuma: 2011.07.13. Legújabb bejegyzések
Hi Devi,

Well I too encountered the same problem and what solution I applied was to put the server side validation on unique filed (if available) like employee id for every insertion and if user tries to re-insert the value by refreshing the page you can show error message on JSP.

Regards,
Henal Saraiya
thumbnail
Sagar A Vyas, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Liferay Master Bejegyzések: 679 Csatlakozás dátuma: 2009.04.17. Legújabb bejegyzések
HI All,

Also want to share why this problem is happen emoticon

Normal scenario is something as below

doView >> Render Default jsp page >> Display view.jsp (or any default jsp)

Client (Browser) send request for some action >> as action fire URL at browser is change >> request come to server >> server will perform operations and send back to response or just status code 200 [successfully] >>will call doView () >> and render same or other jsp page >> But still Browser is unaware with this it has still old URL (actionURL)
And here problem get start. though DB event done successfully but browser having same URL.

So By Putting My above solution will generate below happy scenario

Client (Browser) send request for some action >> as action fire URL at browser is change >> request come to server >> server will perform operations and send back to response or just status code 200 [successfully] >> now we send redirect it again via sendRedirect(redirectTo) >> Now again response go to client having old URL and hit change URL of browser as it was before sending actionURL

Hope this help.

Thanks ,
Sagar Vyas
thumbnail
Jitendra Rajput, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Liferay Master Bejegyzések: 875 Csatlakozás dátuma: 2011.01.07. Legújabb bejegyzések
Agree with u sagar ...by using sendRedirect() we can avoid duplication problem.
But what if i want to set few renderParameter() before sendRedirect() in that case it will throw error like "render parameter has been already set"..
I am not sure why this problm is coming ..
I had already posted thread but didnt get much information on this
For more information look into

http://www.liferay.com/community/forums/-/message_boards/message/10294343
thumbnail
Sagar A Vyas, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Liferay Master Bejegyzések: 679 Csatlakozás dátuma: 2009.04.17. Legújabb bejegyzések
Jitendra Rajput:
Agree with u sagar ...by using sendRedirect() we can avoid duplication problem.
But what if i want to set few renderParameter() before sendRedirect() in that case it will throw error like "render parameter has been already set"..
I am not sure why this problm is coming ..
I had already posted thread but didnt get much information on this
For more information look into

http://www.liferay.com/community/forums/-/message_boards/message/10294343


Hi Jitendra,

I Have ans your post please check it .
http://www.liferay.com/community/forums/-/message_boards/message/10349345


Ans of
But what if i want to set few renderParameter() before sendRedirect() in that case it will throw error like "render parameter has been already set"..


Consider following scenario :
(1) view.jsp >> (2) some action URL >> (3) doView() >> xyz.jsp(same jsp)

Now you are doing renderParameter() at stage (2) that means you want set parameter which will be accessible at doView() method.

Same thing you can do it with sendRedirect(redirectURL); and set your parameter in redirectURL it self. this will give you your expected output emoticon

It will not refresh page and you will get your parameter in redirectURL (which you suppose generate at your jsp side.)

Thanks,
Sagar Vyas
thumbnail
Sagar A Vyas, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Liferay Master Bejegyzések: 679 Csatlakozás dátuma: 2009.04.17. Legújabb bejegyzések
devi nimmagadda:
Hi,


can anybody know this....

How to prevent duplicate submission of a form ?

Because I am submitting a form with data. When I click on submit button the row is inserted into the database table.

When I refresh the page or go to back button of the same page, The row is inserted again.

Can anybody give suggestion to this.....

Multiple rows are inserting in my table.

Thanks in advance



This is well know problem in Servlet called "Not Idempotent".

Whenever you use actionURL and if it is dealing with your DB then try to build up your business logic in such manner that it should "Idempotent".

Here is solution that I usually use in my business logic hope it will help lot of people.

Step 1)you have to do some stuff in actionURL at your jsp page :

<portlet:actionURL var="addReviewCommentURL" name="myMethod">
<portlet:param name="redirectTo" value="<%=PortalUtil.getCurrentURL(request) %>"></portlet:param>
</portlet:actionURL>


Set one parameter called redirectTo (You can give any name) we are passing current URL.
Step 2) Now at controller side you have do something as below.



@ProcessAction(name = "myMethod")
public void myMethod(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException
{

// some of my business logic
String redirectTo = ParamUtil.getString(actionRequest,"redirectTo");
actionResponse.sendRedirect(redirectTo);

}




Thanks,
Sagar Vyas
thumbnail
Jitendra Rajput, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Liferay Master Bejegyzések: 875 Csatlakozás dátuma: 2011.01.07. Legújabb bejegyzések
In Liferay 6 you can avoid duplicate refresh by setting <action-url-redirect> parameter in LiferayPortlet.xml ...
It will avoid dupliacte submission of form
chandra sekhar, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Regular Member Bejegyzések: 149 Csatlakozás dátuma: 2009.07.07. Legújabb bejegyzések
Hi,

* We have to modify our java to use a redirect instead of a forward
* A forward still submits to the page that it lands on. But a redirect will no longer retain the submit state on the landing page

Comment out the forward line
//setForward(req, "portlet.XXX.XXX.success");

replace with these lines

PortletURL redirectURL = ((ActionResponseImpl) res).createRenderURL();
redirectURL.setParameter("struts_action", "/ext/XXX/success");
res.sendRedirect(redirectURL + "&title=" + XXXTitle);

* Make necessary imports
Phoenix Zerin, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Junior Member Bejegyzések: 60 Csatlakozás dátuma: 2011.06.28. Legújabb bejegyzések
Is ActionResponseImpl present in Liferay 6? I seem to be getting NoClassDefFoundError when I use it in my portlet:

21:54:26,538 ERROR [jsp:154] java.lang.NoClassDefFoundError: com/liferay/portlet/ActionResponseImpl
thumbnail
Sagar A Vyas, módosítva 12 év-val korábban

RE: Duplicate Submission of Form

Liferay Master Bejegyzések: 679 Csatlakozás dátuma: 2009.04.17. Legújabb bejegyzések
Is ActionResponseImpl present in Liferay 6? I seem to be getting NoClassDefFoundError when I use it in my portlet:


Yes this class is available in Liferay 6 at below location.

com.liferay.portlet.ActionResponseImpl

But it is inside the IMPL so you can not use it in your plugins portlet.

Thanks,
Sagar Vyas
Ketan Patel, módosítva 11 év-val korábban

RE: Duplicate Submission of Form

Junior Member Bejegyzések: 72 Csatlakozás dátuma: 2012.05.04. Legújabb bejegyzések
Hi!

How to prevent duplicate submission of a form ?


In your liferay-portlet.xml file enter the following line

<action-url-redirect>true</action-url-redirect>
in your portlet tag.

Thanks,
Ketan Patel
http://www.solutionanalysts.com
Shashant Panwar, módosítva 9 év-val korábban

RE: Duplicate Submission of Form

Junior Member Bejegyzések: 28 Csatlakozás dátuma: 2012.09.21. Legújabb bejegyzések
Thanks Ketan, your solution works great with SpringMVC portlets.
"sendRedirect" does not work with SpringMVC portlets due to SpringMapping Interceptor.

Regards,
Shashant
Cristian Keller, módosítva 11 év-val korábban

RE: Duplicate Submission of Form

New Member Bejegyzések: 3 Csatlakozás dátuma: 2012.01.12. Legújabb bejegyzések
We have a problem with double post on update user form with IE7/IE8

We have resolved in this way:

in file edit_user.jsp
We had:
String taglibOnSubmit = renderResponse.getNamespace() + "saveUser('" + ((selUser == null) ? Constants.ADD : Constants.UPDATE) + "');";
...
<aui:form method="post" name="fmpippo" onSubmit="<%= taglibOnSubmit %>">
...
<aui:button type="submit" />

Now, We set:

String taglibOnSubmit = renderResponse.getNamespace() + "saveUser('" + ((selUser == null) ? Constants.ADD : Constants.UPDATE) + "');return false;";
...
<aui:form method="post" name="fmpippo" onSubmit="">
...
<aui:button type="submit" onClick="<%= taglibOnSubmit %>"/>


Also, we have add "return false" on taglibOnSubmit and move the call from form to buttonForm

I dont know if is a correct solution, but in our case this prevent double post submission


Cristian Keller