I followed the first answer to this post. This let me send the error in the servlet to appear in a box on the jsp when it is loaded after the servlet. The problem is that now if I load the jsp without sending the servlet there is still a pop message that says ‘null’.
From the answer to the post I did the following:
I set the attribute in the servlet:
request.setAttribute("alertMsg", "data add sucess");
Then I redirect to jsp using requestDispatcher:
RequestDispatcher rd=request.getRequestDispatcher("/index.jsp"); rd.include(request, response);
Added this scriptlet tag to the jsp:
<% String message = (String)request.getAttribute("alertMsg");%>
Added this js script to the jsp:
I tried different variations of this script, like checking if message is null, using !=, !== instead of equals, or just if(msg) but nothing worked. This might be a stupid mistake in my syntax. I am using eclipse and there is a red x next to the line:
However, it does not give me any message for what is wrong when I hover over it or click it as usual (also nothing on the console).
I would post this as a comment on the answer but I do not have enough reputation for this. Any help is appreciated