将 var javascript 转换为 java 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20291492/
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 00:53:52  来源:igfitidea点击:

Convert var javascript to java variable

javajavascripteclipsejsptomcat

提问by iambeginner

i am using jsp format eclipse 3.2 tomcat server 5.5. I have a problem to convert var variable (javascript) to int variable(java). what is the correct way to do this?

我正在使用 jsp 格式 eclipse 3.2 tomcat server 5.5。我在将 var 变量(javascript)转换为 int 变量(java)时遇到问题。这样做的正确方法是什么?

In html body, i use this method to throw:

在 html 正文中,我使用此方法抛出:

    String qwerty = Integer.toString(f);
    String asdf = Integer.toString(d);
            System.out.println("CONVERTED VALUE : "+qwerty+" "+asdf);
    %>
    <input type="hidden" id="balance" name="balance" value="<%=qwerty%>" />
    <input type="hidden" id="loop" name="loop" value="<%=asdf%>" />
    <%
    System.out.println("VALUES : "+balance+" "+loop);

In html head(script):

在 html 头(脚本)中:

            <script>
            function myfunction()
            {
            var looping = document.getElementById("loop").value;
        var balancing = document.getElementById("balance").value;

        <%
            String loop="<%=document.writeln(looping)%>";
        String balance="<%=document.writeln(balancing)%>";

        int balance = Integer.parseInt(balancing);
        int loop = Integer.parseInt(looping);

            %>
            ..........................cont

how to convert this var to string?

如何将此var转换为字符串?

采纳答案by skuntsel

You need to understand that Java is run at the server side, while JavaScript is run at the client side. They are running in different contexts and tiers and most likely physical machines (cheers, localhost). In the context of your question, java code is used to produce HTML/JS (on the server) that is used to communicate with the end user typically within the browser (on the client). So they don't "see" each other nicely/straightforwardly.

您需要了解 Java 在服务器端运行,而 JavaScript 在客户端运行。它们运行在不同的上下文和层中,最有可能是物理机器(欢呼,本地主机)。在您的问题的上下文中,Java 代码用于生成 HTML/JS(在服务器上),用于与最终用户通常在浏览器内(在客户端)进行通信。所以他们不会很好地/直接地“看到”彼此。

As soon as you understand these facts and remember the basics of HTTP, the server-client communication is handled by means of request-response model. In this light you can "print" data in your view file to send information to the client, as in var passedFromJava = ${bean.data};so that it ends up in response body. Reversely, when client fires the request (submits the form with e.g. inputelement) all of the inputs of the submitted form end up as request parameters and could be obtained in Java as String fromHtmlPage = request.getParameter("inputName");. You can of course add such parameters from JavaScript side on in, for instance <input type="hidden">element, add them as data to an AJAX request, etc. But do understand that direct JavaScript is invisible to Java: communication means is the HTTP.

一旦您理解了这些事实并记住了 HTTP 的基础知识,服务器-客户端通信就会通过请求-响应模型进行处理。在这种情况下,您可以在视图文件中“打印”数据以将信息发送到客户端,var passedFromJava = ${bean.data};以便它以响应正文结束。相反,当客户端触发请求(提交带有 eginput元素的表单)时,提交表单的所有输入最终都会作为请求参数,并且可以在 Java 中作为String fromHtmlPage = request.getParameter("inputName");. 你当然可以从 JavaScript 端添加这样的参数,例如<input type="hidden">元素,将它们作为数据添加到 AJAX 请求等。但是要明白直接 JavaScript 对 Java 是不可见的:通信方式是 HTTP。

By the way, usage of scriptlets (%) is not welcome nowadays. For more information consult the classics: How to avoid Java code in JSP files?.

顺便说一句,%现在不欢迎使用 scriptlet ( )。更多信息请参阅经典:如何避免 JSP 文件中的 Java 代码?.