This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSP and the fly likes Refreshing a page causes a duplicate row to be inserted pls help...???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » JSP
Bookmark "Refreshing a page causes a duplicate row to be inserted pls help...????" Watch "Refreshing a page causes a duplicate row to be inserted pls help...????" New topic

Refreshing a page causes a duplicate row to be inserted pls help...????

Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

hi ppl,
i am working on a application based on the struts architecture. every relevent action on a page does its processing thru the controller servlet and action class and the controller servlet then forwards the result to the result jsp using the requestdispatcher method.
due to this i am facing a problem in pages where i am inserting a row in the database when ever i refresh a page just after i have inserted a row in the database a duplicate row is inserted due to obvious reasons being the url still being the one which did the insert action in the first case coz of the requestdispatcher .....
now the solution to the prob is if i can differentiate between a post action thru submit on a page and a post action coz of refresh i will be able to prevent the unwanted results...and the problem lies here i just cannot think or come up with a way where in i can kno if the post was due to a submit or refresh .......
i kno this sounds very trivial or maybe not but i need to kno of something which will help me differentiate this at the controller servlet level so that i can then do the subsequent procesing accordingly...
thanking u all in advance ...

ps: i kno of a technique which checks for the header content but it works only in I.E but fails in netscape pls can somebody suggest a cross browser compatable solution to this.....

Joined: Jan 20, 2002
Posts: 17
do one thing
put ur sql insert statement in a file as page1.jsp and redirect to the page page2.jsp which contains message like thanks etc. from page1.jsp.
to redirect use response.sendRedirect( "page2.jsp" );
hope this will solve ur problem
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63206

Sort of, but breaking your Model 2 architecture by polluting your JSP page with SQL is not necessary to solve the problem.
There a number of ways around this problem: one is to use a session token to determine if the form that causes the intial submission has already been processed.
Another, which I've seen more often is to use a redirect and NOT directly forward to the view from the servlet that performed the operation that you don't want to duplicate.
For example: let's say that after you insert the row, you want to display a page that shows the entity represented by that row. In your insertion servlet you would perform the SQL insert operation, but then you would NOT directly foward to the display page. You would redirect to a servlet that would obtain the result of the insertion and then that servlet forwards to the display page.
That way, upon a refresh, all that happens is that the servlet re-obtains the record and displays it. No multiple insertions can occur.
[ November 30, 2002: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

thanx a lot bear...
we were following the model where the second servlet takes over the processing once the insertion is done ....but we had to discard it as if u use a response.sendRedirect() the request gets broken and any data now stored in the request is lost ..we are using the request object to store the content to be displayed on the jsp ...(the session is strictly to be avoided to store this info as the clean up will be troublesome )
the flow is somewhat like this
jsp -- servlet -- actionclass -- stateless-ejb -- DAXfactoryclass -- dax --database
with the servlet doin the controlling ...
still i will look in to what u said and i think i will temporarily store the data in the session while i redirect the flow from controllerservlet to the second servlet.....
thanks for the help as i think this being completely on the server side browser independence will be automatically obtained....
I agree. Here's the link:
subject: Refreshing a page causes a duplicate row to be inserted pls help...????
It's not a secret anymore!