File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Issue with request.getParameter() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Issue with request.getParameter()" Watch "Issue with request.getParameter()" New topic

Issue with request.getParameter()

Sumithab Baskaran
Ranch Hand

Joined: Oct 29, 2004
Posts: 52
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

Joined: Jul 08, 2003
Posts: 24199

Moving to Servlets forum.

[Jess in Action][AskingGoodQuestions]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

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()?

[Asking smart questions] [About Bear] [Books by Bear]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

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.

[OCA 8 Book] [Blog]
I agree. Here's the link:
subject: Issue with request.getParameter()
jQuery in Action, 3rd edition