This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Issue with request.getParameter()" Watch "Issue with request.getParameter()" New topic
Author

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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Moving to Servlets forum.


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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with request.getParameter()
 
Similar Threads
Getting duplicate records when the page refreshes.
Card layout
related to cardLayout
how to differ between new request and page refresh..
JDOM Issue.... Please Help..