Reputation: 899
I am trying to show an alert in my jsp page with the following code.
<%
---------------
---------------
out.write("<script type='text/javascript'>\n");
out.write("alert('Hello')");
out.write("</script>\n");
---------------
---------------
%>
Everything is working fine. Alert is coming when page is loading. Now I want to show an alert with string variable. For this I changed the above code to below. Now the alert is not coming.
<%
---------------
---------------
String name = (String) application.getAttribute("name");
System.out.println("name = "+name);
out.write("<script type='text/javascript'>\n");
out.write("alert('Hello'+name)");
out.write("</script>\n");
---------------
---------------
%>
Could you please tell me how to add string variable to the above alert function.
Upvotes: 0
Views: 10665
Reputation:
You can try this with EL without jsp scriplets:
<script>
alert('Hello ${applicationScope.name}');
</script>
OR
<script>
alert('Hello ${name}');
</script>
P.S. Do not use application scope to store user specific values, its shared among the different sessions(users). Try HttpSession or HttpServletRequest instead.
EDIT
<%
out.write("<script>");
out.write("alert(\"Hello " + application.getAttribute("name") + "\")");
out.write("</script>");
%>
Upvotes: 1
Reputation: 8021
Correct your code like below
out.write("alert('Hello+" + name + "')");
Upvotes: 2