在jsp scriptlet中获取javascript变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22922245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:02:19  来源:igfitidea点击:

get javascript variable value in jsp scriptlet

javajavascriptjspel

提问by participantjava

I have a scenario where i need to compare javascript varibable with java object inside scriptlet of jsp page. How can i get javascript variable in the jsp scriptlet or the other way will also work for me(getting arraylist object value in javascript).

我有一个场景,我需要将 javascript 变量与 jsp 页面的 scriptlet 内的 java 对象进行比较。我如何在 jsp scriptlet 中获取 javascript 变量或其他方式也适用于我(在 javascript 中获取 arraylist 对象值)。

回答by Luiggi Mendoza

If you need a variable from server in JavaScript, use Expression Language (assuming this variable is stored as request, session, application or page attribute):

如果您需要来自服务器的 JavaScript 变量,请使用表达式语言(假设此变量存储为请求、会话、应用程序或页面属性):

<%@taglib uri="http://example.com/functions" prefix="f" %>

<!-- ... -->

function showValues(str) {
    var empListLength = ${empty empList ? 0 : fn:length(empList)};
}

If you need a variable from JavaScript in your scriptlet, you're out of luck, this is impossible. The only way to get a variable from an external resource is as a request parameter.

如果您的 scriptlet 中需要来自 JavaScript 的变量,那您就不走运了,这是不可能的。从外部资源获取变量的唯一方法是作为请求参数。



I omit the answer using scriptlets directly because its usage is highlydiscouraged.

我省略了直接使用 scriptlet 的答案,因为它的使用是非常不鼓励的

回答by Anthony_Michael

Javascript is run in the browser and JSP's are run on the server. Declare the java script variable using scriptlets and then compare on the browser:

Javascript 在浏览器中运行,而 JSP 在服务器上运行。使用 scriptlet 声明 java 脚本变量,然后在浏览器上进行比较:

var x = <%= str%>

var x = <%= str%>

Then you can compare in the browser during execution of the javascript. Other than that scriptlets are unaware of anything in JavaScript because it has to be run in the broswer and by that time the scriptlets have already been executed and converted to html, javascript, or css.

然后您可以在执行 javascript 期间在浏览器中进行比较。除此之外,scriptlet 不知道 JavaScript 中的任何内容,因为它必须在浏览器中运行,而到那时,scriptlet 已经被执行并转换为 html、javascript 或 css。

回答by Gautam Viradiya

It is possible, two ways to assign javascript variable value to jsp scriptlet.

可以通过两种方式将 javascript 变量值分配给 jsp scriptlet。

First WayDemo1.jsp

第一种方式Demo1.jsp

<script>
    var name = "Gautam";
</script>
<%
    String str = "<script>document.writeln(name)</script>";
    out.println("value: " + str);
%>

Second WayDemo2.jsp

第二种方式Demo2.jsp

<html>
<script>
    function call() 
    {
        var name = "Gautam";
        window.location.replace("Demo2.jsp?name=" + name);
    }
</script>
<input type="button" value="Get" onclick='call()'>
<%
    String name = request.getParameter("name");
    if (name != null) 
    {
        out.println(name);
    }
%>
</html>

回答by Darshan Malani

If you need a variable from server in JavaScript, use Expression Language(assuming this variable is stored as request, session, application or page attribute):

如果您需要来自服务器中的变量JavaScript,请使用Expression Language(假设此变量存储为请求、会话、应用程序或页面属性):

If you need a variable from JavaScriptin your scriptlet, it is impossible. The only way to get a variable from an external resource is as a request parameter

如果您需要一个变量JavaScript在你的scriptlet,这是不可能的。从外部资源获取变量的唯一方法是作为请求参数