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 ing 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 ing.
<%
---------------
---------------
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.
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 ing 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 ing.
<%
---------------
---------------
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.
Share edited Feb 10, 2015 at 7:15 user2636874 asked Feb 10, 2015 at 5:54 user2636874user2636874 8996 gold badges15 silver badges40 bronze badges2 Answers
Reset to default 2Correct your code like below
out.write("alert('Hello+" + name + "')");
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>");
%>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744935458a4601993.html
评论列表(0条)