0

Given two jsp's: myFirstJsp.jsp, mySecondJsp.jsp

myFirstJsp.jsp receives the submit parameters from another form, stores some values in the DB, and if everything is successful makes a forward to mySecondJsp.jsp, sending a couple of parameters like this:

<jsp:forward page="mySecondJsp.jsp">
<jsp:param name="param1" value='<%=request.getParameter("param1")%>'/>
<jsp:param name="param2" value='<%=request.getParameter("param2")%>'/>
</jsp:forward>

Everything works fine, but if the user press F5 or refresh the page, an exception will occur, since the URL would still be pointing to "myFirstJsp.jsp", so basically a duplicate insert exception will occur, and I want to prevent this.

The other option available is to use sendRedirect instead of forward, like this:

response.sendRedirect( "mySecondJsp.jsp?param1=somevalue&param2=somevalue" );

however, in this case the parameters will be visible to the user, and although normally they won't change them, I need to prevent that scenario too.

Is there any way to avoid either situations?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Sergio
  • 658
  • 1
  • 9
  • 22
  • Refer http://stackoverflow.com/questions/17001185/pass-hidden-parameters-using-response-sendredirect – Anptk Feb 27 '15 at 04:22

1 Answers1

0

You can use following to set a request parameter in myFirstJsp.jsp and get it on mySecondJsp.jsp:

request.setAttribute("param_name","param_value");
request.getAttribute("param_name");

Similarily, you can put the values in session.

But, if you want to prevent duplication of records in table then you can write a procedure in database to handle this.

Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
  • Thanks a lot!! Duplicate records is not an issue. The attempt of doing it is what I was trying to avoid, so users don't get an error/exception. So you're saying that I can use sendRedirect with these setAttribute/getAtrribute?? – Sergio Mar 01 '15 at 01:23
  • No, you cannot use request.setAttribute/getAtrribute with sendRedirect. In sendRedirect case, only session.setAttribute/getAtrribute can help you. – Brijesh Bhatt Mar 02 '15 at 08:58