将var javascript转换为java变量 [英] Convert var javascript to java variable

查看:248
本文介绍了将var javascript转换为java变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jsp格式的eclipse 3.2 tomcat服务器5.5。我有一个问题,将变量(javascript)转换为int变量(java)。这样做的正确方法是什么?



在HTML体中,我使用这种方法抛出:

  String qwerty = Integer.toString(f); 
String asdf = Integer.toString(d);
System.out.println(CONVERTED VALUE:+ qwerty ++ asdf);
%>
< input type =hiddenid =balancename =balancevalue =<%= qwerty%> />
< input type =hiddenid =loopname =loopvalue =<%= asdf%> />
<%
System.out.println(VALUES:+ balance ++ loop);

在html头(脚本)中:

 < script> 
函数myfunction()
{
var looping = document.getElementById(loop)。
var balancing = document.getElementById(balance)。

<%
String loop =<%= document.writeln(循环)%>;
String balance =<%= document.writeln(balancing)%>;

int balance = Integer.parseInt(balancing);
int loop = Integer.parseInt(循环);

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

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

解决方案

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



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



顺便说一下,使用scriptlet()不受欢迎。有关更多信息,请参考经典:如何避免JSP文件中的Java代码?


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?

In html body, i use this method to throw:

    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):

            <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?

解决方案

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.

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. 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");. 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.

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

这篇关于将var javascript转换为java变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆