Take the 2-minute tour ×

I have an index.jsp page where I have a form where the user enters some data that gets stored in the database using a Controller servlet.

I want to display the same page (index.jsp) with that form after entering the data in the database. Also, I want to display all the entries that the user entered in the database.

I tried using the forward() method of RequestDispatcher. It works fine (meaning I am able to display that same form again and also display all the data entered by that user below the form using JSTL).

But the problem is whenever the user presses the Refresh or F5 button, all the previous data also gets entered in the database and as I am displaying all the data, and that duplicate entries also come up.

I thought of using the POST-REDIRECT-GET pattern, but the problem is when I redirect I don't get those data to be displayed using JSTL.

How do I do it?

share|improve this question

4 Answers 4

up vote 8 down vote accepted

I thought of using POST-REDIRECT-GET pattern but the problem is when I redirect I don't get those data to be displayed using JSTL.

Just send a request parameter along identifying the information you'd like to display in the new GET request.

// ...
Long id = dataService.save(data);
// ...
response.sendRedirect(request.getContextPath() + "/index?editId=" + id);

and then in the servlet which is mapped on an URL pattern of /index

    Long editId = Long.valueOf(request.getParameter("editId")); // Handle nullcheck yourself.
    Data editData = dataService.find(editId);
    request.setAttribute("editData", editData); // For the edit form.
    List<Data> allData = dataService.list();
    request.setAttribute("allData", allData); // For the table/list of all data.
share|improve this answer
Thank you Balus I think this is a good solution I will definitely try this and let you know. –  Abubakkar Rangara Oct 11 '12 at 4:32
It works perfectly! –  Abubakkar Rangara Oct 11 '12 at 4:59
You're welcome. –  BalusC Oct 11 '12 at 10:43

I would add an invisible ID to the page. If the data is new to the database (ID = unknown), insert and create an ID and update the page with the ID. That way you know if it is a ID != unknown, and you don't have to do an insert. And if the data hasn't changed, you don't even have to do an update...

share|improve this answer
can you please point or suggest to that better ways –  Abubakkar Rangara Oct 4 '12 at 10:23
I'm afraid, I'm not experienced with servlets. What I wrote was a typical scenario that I am facing using plain old Java and databases. I guess you could adopt that approach. What I cannot tell is if servlet technology has some built-in feature doing this for you. –  Fildor Oct 4 '12 at 10:45
Fildor's suggestion is the way to do it. Include the database ID of your record in the page in a hidden field. If the user hasn't submitted the data yet this should be -1 or 0. When you process the request to insert the data get the database ID of the newly inserted row and include that in the page next time. If the user submits the form again use the ID to update the details, which is what they would expect. BTW, use a POST not a GET in your form - the user will then be alerted that the data will be resubmitted if they refresh. –  Martin Wilson Oct 4 '12 at 10:54

Here is the simplest solution


//This List is just for demonstration purposes
    session.setAttribute("nameList", names);
    //Here you put your database insert code

    //Whenever the code execution reaches this line , 
    //then it means that you have a new page submission
    //and not a refresh or f5 case


<!DOCTYPE html>
                <c:forEach items="${nameList}" var="element">    
                        <td>Name:  <c:out value="${element}"/> </td>

The trick lies with the response.sendRedirect("index.jsp");. This makes null all request parameters. If an f5 or refresh is hit, then the if is never executed. If a normal submit is the case, then the if is executed and you call the response.sendRedirect("index.jsp");.

In summary, all you really need to do is:

1) Check if(request.getParameter("name")!=null )

2) If above is true then do database inserts

3) If above is true then response.sendRedirect("index.jsp");


    ArrayList<String> currentList = DbUtility.getAllNamesFromDb();
    session.setAttribute("nameList", currentList);

You just have to implement these 2 methods. The addNameToDb(String) will make the insert in your database. And the getAllNamesFromDb() will return an ArrayList<String> Object that will represent on the entries from your database. (And you no longer need the names list introduced before in my first answer)

share|improve this answer
Hi thanks for your reply, but I also want to add entries to the database.What I want to do is suppose I made 1 entry using the form in the database, then all my entries(assume name is the primary key) should be displayed below the same form that I used. It is displaying fine for now but when I hit refresh I find the duplicate entries also. –  Abubakkar Rangara Oct 8 '12 at 8:18
see my UPDATE on the logic –  MaVRoSCy Oct 8 '12 at 12:57
I will try it but I don't want to use session scope for storing my data it needs to be in request scope. –  Abubakkar Rangara Oct 8 '12 at 13:21
no problem. use request. It will work just fine –  MaVRoSCy Oct 8 '12 at 13:35

I think PRGP (Post Redirect Get Pattern) is the way to go for this. If you're using Spring Web Flow it has a FlashScope where you can put the data that you want to be retained after the Post-get-redirection. You can retain more than just an edit Id using this approach.

share|improve this answer

Your Answer


