如何在JSP中将JavaScript值传递给Scriptlet?

use*_*367 19 javascript java jsp scriptlet

谁能告诉我如何在JSP中将JavaScript值传递给Scriptlet?

har*_*tps 17

我可以提供两种方式,

a.jsp,

<html>
    <script language="javascript" type="text/javascript">
        function call(){
            var name = "xyz";
            window.location.replace("a.jsp?name="+name);
        }
    </script>
    <input type="button" value="Get" onclick='call()'>
    <%
        String name=request.getParameter("name");
        if(name!=null){
            out.println(name);
        }
    %>
</html>
Run Code Online (Sandbox Code Playgroud)

b.jsp,

<script>
    var v="xyz";
</script>
<% 
    String st="<script>document.writeln(v)</script>";
    out.println("value="+st); 
%>
Run Code Online (Sandbox Code Playgroud)

  • b.jsp建议不起作用,它打印`<script> document.writeln(v)</ script>` (5认同)

Joe*_*ckx 12

您的javascript值是客户端,您的scriptlet正在运行服务器端.因此,如果您想在scriptlet中使用javascript变量,则需要提交它们.

要实现此目的,请将它们存储在输入字段中并提交表单,或执行ajax请求.我建议你研究一下JQuery.