Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

Convert var javascript to java variable

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...