• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with request.getParameter()

 
Sumithab Baskaran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Servlets forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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()?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4028
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic