简体   繁体   English

将var javascript转换为java变量

[英]Convert var javascript to java variable

i am using jsp format eclipse 3.2 tomcat server 5.5. 我正在使用jsp格式的eclipse 3.2 tomcat服务器5.5。 I have a problem to convert var variable (javascript) to int variable(java). 我将var变量(javascript)转换为int变量(java)时遇到问题。 what is the correct way to do this? 正确的方法是什么?

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 head(script)中:

            <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转换为字符串?

You need to understand that Java is run at the server side, while JavaScript is run at the client side. 您需要了解Java在服务器端运行,而JavaScript在客户端运行。 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). 就您的问题而言,java代码用于(在服务器上)生成HTML / JS,该HTML / JS通常用于在浏览器(在客户端)上与最终用户进行通信。 So they don't "see" each other nicely/straightforwardly. 因此,他们不会很好/直接地“看到”对方。

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. 一旦了解了这些事实并记住了HTTP的基础知识,服务器-客户端通信便通过请求-响应模型进行处理。 In this light you can "print" data in your view file to send information to the client, as in var passedFromJava = ${bean.data}; 因此,您可以在视图文件中“打印”数据以将信息发送给客户端,如var passedFromJava = ${bean.data}; so that it ends up in response body. 因此它最终出现在响应主体中。 Reversely, when client fires the request (submits the form with eg input element) 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"); 相反,当客户端触发请求(使用例如input元素提交表单)时,提交的表单的所有输入最终都作为请求参数,并且可以在Java中以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. 当然,您可以从JavaScript端添加此类参数,例如<input type="hidden">元素,将它们作为数据添加到AJAX请求中,等等。但是请务必理解直接JavaScript对Java是不可见的:通信方式是HTTP。

By the way, usage of scriptlets ( % ) is not welcome nowadays. 顺便说一句,如今不欢迎使用scriptlet( % )。 For more information consult the classics: How to avoid Java code in JSP files? 有关更多信息,请参阅经典著作: 如何避免JSP文件中的Java代码? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM