1

Here is my code.

<p id="error_2" align="center">${error}</p>
<script>
    setTimeout(function() {
        var errorMsg = document.getElementById("error_2");
        errorMsg.style.display = "none";
        alert(errorMsg.innerHTML);
        // Clear the value of the 'error' variable in the JSP context
    }, 5000);
</script>

${error} is used to retrieve information from a Java Servlet: In my Java Servlet, there is this code:

request.setAttribute("error", "This is a msg");
request.getRequestDispatcher("signup.jsp").forward(request, response);

So when the user clicks the button (not shown), it will show "This is a msg". I made the message only show for 5 seconds before disappear. These all works fine. However, when the user refreshes the page, the error message shows again, and it's not desired.

I think the reason is that in the Java servlet, I setAttribute("error", "This is a msg"), but I don't know how to set it back as "" in the javascript.

prasad_
  • 12,755
  • 2
  • 24
  • 36
William Li
  • 11
  • 2
  • Hi, you might need to write ajax which will call your server and there you can empty the "error" attribute value . – Swati May 03 '23 at 03:15
  • When you click the button you are sending a request. The servlet program runs, sets the request attribute and the JSP page is displayed where you are showing the request attribute set on the servlet (server). Your JavaScript resets the HTML on the browser - it does not reset the request attribute. The page refresh is likely sending a new request, which runs the servlet again, sets the request attribute and so you see the same message. Why do you want to refresh the page? _I suggest you search for the topic "servlet avoid refresh jsp page refresh" for more insight into the issue._ – prasad_ May 03 '23 at 03:29
  • Thank you everyone, I will need to look more into the linked post, but it’s really helpful. – William Li May 03 '23 at 03:58

0 Answers0