PeakGen
PeakGen

Reputation: 23025

Refreshing a JSP form resubmits the data

I have a jsp page called patient.jsp with a Form which is a pop-up. This form is submitted using post method. Once this form reached the servlet, something like below take place.

request.setAttribute("id",id);
RequestDispatcher dispatch = getServletContect().getRequestDispatcher("/patient.jsp");
dispatch.forward(request,response);

There is a big problem. Once this is forwarded back to the patient.jsp, if the user refresh the web page, everything he previously entered into the forms will be resubmitted and saved in the database.

We used RequestDispatcher because we have to pass an attribute from Request scope. Any idea how to solve this?

Upvotes: 0

Views: 1245

Answers (1)

reformy
reformy

Reputation: 1027

First you should redirect and not forward:

response.sendRedirect("patient.jsp");

Make sure the relative path is correct.

From here you have two options:

  1. Set the attribute in the session and not in the request, then you can get it in the jsp. Of course you need to deal with parallel requests using this, so the name of the attribute should be unique each time.
  2. Send the attribute as a http get parameter (if it is serializable): response.sendRedirect("patient.jsp?id=273");

Upvotes: 1

Related Questions