Discussions

Web tier: servlets, JSP, Web frameworks: problem with jsp:forward when user refreshes the browser

  1. this is my problem:
    i insert a value into the database and forward it another page which displays the result.
    since i use jsp:forward, the user browser displays the url from which the jsp:forward was called.
    if i refresh the browser, the data are again inserted to the database. is there a way to prevent it.
  2. i'm sure there are more elegant solutions, but here's one that kind of works:

    test.jsp:

    <%
    if (session.getAttribute("forwarded") != null) {
        out.println(session.getAttribute("forwarded"));
    } else {
    %>
    <jsp:forward page="/status.jsp">
    <jsp:param name="msg" value="Ok, data inserted. don't hit refresh..." />
    </jsp:forward>
    <% } %>

    status.jsp:

    <%= request.getParameter("msg") %>
    <%
    session.setAttribute("forwarded", "Please do not hit refresh!");
    %>


    Of course now you're burdened with keeping track of the 'forwarded' attribute of the session object.
  3. thank u. this one is fine for my application.