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.