最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Convert var javascript to java variable - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Nov 29, 2013 at 18:20 iambeginneriambeginner 131 gold badge2 silver badges4 bronze badges 3
  • 3 The server side Java code in a JSP page runs first and generates an expanded template, which is then sent to the client and executed there. So you can't call the Java from JavaScript directly as you appear to be trying. You either need to rethink the logic, or send the client-side data to the server in a new request. – David Conneely Commented Nov 29, 2013 at 18:28
  • This thread wants to be a canonical explanation fo this kind of problem: programmers.stackexchange./questions/171203/…. – 11684 Commented Nov 29, 2013 at 18:34
  • so can i use request.getParameter in a same page of jsp? – iambeginner Commented Nov 29, 2013 at 18:40
Add a ment  | 

1 Answer 1

Reset to default 3

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 municate 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 munication 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: munication means is the HTTP.

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

发布评论

评论列表(0)

  1. 暂无评论