Home » Javascript » javascript – pop up message box sent through servlet to jsp

javascript – pop up message box sent through servlet to jsp

Posted by: admin February 24, 2020 Leave a comment


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:

<script type="text/javascript">
   var msg = "<%=message%>";

And I tried solving the problem by modifying the javascript posted in the answer to be something like:

<script type="text/javascript">
   var msg = "<%=message%>";

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:

<script type="text/javascript">

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

How to&Answers: