0

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?

PeakGen
  • 21,894
  • 86
  • 261
  • 463

1 Answers1

1

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");
reformy
  • 989
  • 2
  • 10
  • 19
  • about the option 1: I don't think we have to use unique name everytime. Because if we attach the value with key `id` and if we reattach another value with key `id` to the same session, the previous attribute will be replaced, right? – PeakGen Jan 29 '15 at 10:38
  • You are right. I meant that if the user sends two requests at the same time somehow (not a common scenario but possible), the `id` attribute will be set twice and be the same for both responses. A classic sync problem. I would use a key like `id_` and pass the UUID as get parameter to the page. – reformy Jan 29 '15 at 11:38