0

I need to update a javascript array everytime there is and event, like a click intercept with JQuery. I need that new value from JSP array come into an array javascript.

I know there are solutions like this Passing array from .jsp to javascript function. The problem is quite different: code linked run only once, when I need to update every time I intercept an event.

I can understand that problems is inside the translation from Servlet to HTML, it seems: once the Jsp variable take value the first time than they became "static code" and it can't change.

Could you suggest me some alternative or solution?

function update_coordinate_javascript()
{
    <%  
        for (int s: mycord.getXY()[0])  {
    %>
            xyscript.push(<% out.print(s); %>);
    <%
        }   
    %>
}

$("#myB").click(function(){
     $.ajax({
               type: "POST",
                url: "readXY.jsp",  // Some elaborations
                data: {incrementa:true},
                dataType: "html",
                success: function(msg)
                {
                  alert("Ok");
                },
                error: function()
                {
                  alert("Fail");
                }
              });
            update_coordinate_javascript();
            return false;
    });
}

1 Answer 1

1

You cannot mix Javascript and JSP like you're trying in the update_coordinate_javascript function, because Javascript runs client-side and JSP server-side.

What you are doing in that function is printing the values that the JSP has in mycord at the time it runs on the server-side. You are printing those into the Javascript function (view source to confirm) and thus you are printing out Javascript code basically. And once you've done that, you have a Javascript function with a hardcoded list. So every time you call that function you're just populating the same hardcoded list.

Instead: The JSP you are calling in Ajax should print the array to the response either as JSon, XML, or as a string with a certain character reserved to be used as a separator, and Javascript should parse that.

 $.ajax({
           type: "POST",
            url: "readXY.jsp", //print array as JSON, XML, or CSV-like text there
            data: {incrementa:true},
            dataType: "html", //change this to JSON, XML, or text as needed
            success: function(msg)
            {
               //msg here is the response from the JSP you are calling, 
               //so whatever you print to response there 
               //is in this variable
               alert("Ok");
               update_coordinate_javascript(msg); //parse the msg in there
            },
            error: function()
            {
              alert("Fail");
            }
          });

Then obviously you need to take the JSP scriplet code out of update_coordinate_javascript and change it to Javascript code that parses the msg parameter which is passed in. How you will do that will depend on how you decide to format the output from the JSP you are calling in Ajax (i.e. whether you have it return CSV-like text, XML, HTML, or JSON).

So if you go with CSV, it could be as simple as:

function update_coordinate_javascript(msg)
{
    var mycords = msg.split(",");
    for(var i=0; i<mycords.length; i++)
    {
       xyscript.push(mycords[i]);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! That's was the question, and you explain where is the problema and a possibile solution! Thank's! I hope, in future, someone improve integration between Javascript and JSP, allow a parameter exchange without a "bruteforce" parsing.
@robott, Actually that's what JSON (Javascript object notation) does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.