Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中传递jsp字符串变量值并在那里打印_Java_Javascript_Jsp - Fatal编程技术网

如何在javascript中传递jsp字符串变量值并在那里打印

如何在javascript中传递jsp字符串变量值并在那里打印,java,javascript,jsp,Java,Javascript,Jsp,嗨,我想在javascript中使用jsp变量值,我该如何使用 这是我的密码 Demo.jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <

嗨,我想在javascript中使用jsp变量值,我该如何使用

这是我的密码

Demo.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
        String plane="A";
        %>
        <script>
     var planB=<%=plane%>;
     c
     if(planB==B){
     document.write("shakti");
     }else{
     document.write("sharma");
     }
     </script>
</body>
</html>

JSP页面
var planB=;
C
if(planB==B){
文件。书写(“shakti”);
}否则{
文件。书写(“sharma”);
}

如何获得所需的输出

使用以下命令更改脚本块:

<script>
    var planB = '<%=plane%>';
    if(planB === 'B'){
        document.write("shakti");
    } else{
        document.write("sharma");
    }
</script>

var planB=“”;
如果(平面B=='B'){
文件。书写(“shakti”);
}否则{
文件。书写(“sharma”);
}
变化:

  • jsp变量应该在单引号(或双引号)内,否则会出现错误:“A未定义”
  • 脚本块中有一个字符“c”,这也会引发javascript错误
  • 我已将“==”更改为“==”(在if检查中),这样您不仅可以检查值,还可以检查类型
  • 您必须在单引号(或双引号)之间放置“B”(在if检查中),否则将出现错误:“B未定义”。您希望将变量与字符串进行比较,而不是将变量==>放在单引号(或双引号)内
try
var planB=''