• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Issue with request.getParameter()

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All!

I have a comments page. This page must display the list of comments stored in the database. This page contains two buttons. Add comment and Cancel. The Add Comment button takes us to the screen where you can type in a comment and save it.

Issue: When I add a new comment and return back to the comments page, it shows the newly added comment, which is right. When I hit the refresh button on the browser, the newly added comment is added once more. Everytime I click on the refresh buton, the comment keeps adding to the list.

My Action class:
if(request.getParameter("Add Comment")
{
return mapping.findForward("addComment");
} else if(request.getParameter("Cancel") {
return mapping.findForward("cancel");
}
return mapping.findForward("success");

So, After adding the comment and returning to this action class, the refresh button is calling this action in which the request parameter is still set to "addComment" and thats why a new comment is added everytime.

How do I fix this?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Servlets forum.
 
Sheriff
Posts: 67752
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
After your servlet has added the comment to the DB, redirect rather than forward. The refresh is resending the add request every time it is clicked. By preforming a redirect, the most recent action is not the add operation.

What does this have to do with request.getParameter()?
 
author
Posts: 4354
45
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arg, I posted a reply as it was being moved and lost it when the post was move... lets see where I was...

Off hand I'd say its because you keep submitting the same data over and over again. Have you tried, after submitting, to refresh the page not by using the refresh button but by using a bookmark or typing the URL in directly? If it still happens, then I would say you have a left over variable some where.

For Comments pages of websites I like to store the user's IP address and not allow more than one submission per IP address. This is an easy solution that also eliminates spamming from a particular user. If you are using a database to store your comments its even easier since you can just set a primary key or unique-ness constraint on the ip address.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic