How to show javascript alert with string val in jsp - Stack Overflow

I am trying to show an alert in my jsp page with the following code.<%----------------------------

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 badges
Add a ment  | 

2 Answers 2

Reset to default 2

Correct 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

相关推荐

  • How to show javascript alert with string val in jsp - Stack Overflow

    I am trying to show an alert in my jsp page with the following code.<%----------------------------

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信