• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
hth,
bear
[ November 30, 2002: Message edited by: Bear Bibeault ]
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
reply
    Bookmark Topic Watch Topic
  • New Topic