-1

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?

iambeginner
  • 13
  • 1
  • 2
  • 4
  • 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 Nov 29 '13 at 18:28
  • This thread wants to be a canonical explanation fo this kind of problem: http://programmers.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming. – 11684 Nov 29 '13 at 18:34
  • so can i use request.getParameter in a same page of jsp? – iambeginner Nov 29 '13 at 18:40

1 Answers1

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

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67