1

I'm currently trying to have a servlet return an array of arrays to my javascript jquery code. The issue I'm having is that if I want to have an array of arrays in jQuery, I have to say

var derpArray[0] = new array();

This poses a problem since I may not know how many rows I'll be returning from a server. At the moment I'm attempting to have my servlet return an array of array, assign to a javascript variable, then create a table out of the values. Any hints would be appreciated.

Here is my code below:

Java Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String[][] sourceFiles = null;
    System.out.println("PING");

    try{
        sourceFiles = WebAppDB2Connector.getTopRows(5);
    }
    catch (Exception e)
    {
        SimpleLogger.logInfo("Error retrieving data..." + e.getMessage());
    }

    Gson gson = new Gson();
    String json = gson.toJson(sourceFiles);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);

    System.out.println("end");


}

JavaScript:

window.onload = load;

function load() {

$.getJSON("http://localhost:9194/MyWebApplication/SourceFile", function(sourceFileData) {

var table = $("<table></table>");
for (var i = 0; i < sourceFileData.length; i++) 
{
    var line = $("<tr></tr>");
    for (var j = 0; j < sourceFileData[i].length; j++)
    {
        line.append( $(cell).html('<td>' + sourceFileData[i][j] + '</td>'));
    }
    table.append(line);
}

table.appendTo( $("#sourceFileTable"));

});

document.getElementById("test").innerHTML = "LOL";

}

Vernah
  • 81
  • 1
  • 4
  • 11
  • http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Musa Apr 17 '13 at 16:45

1 Answers1

1

EDIT after OP added servlet code.

I doubt that your servlet would work. If it did, it would be coincidence. You will probably need some sort of JSON library to use with your servlet. json.org/ provides a simple JSON library, which is probably good enough for your needs. For more advanced features you can look at libraries like Jackson or JSONSimple.

On the front end, You'll also want to add your logic as a callback to the get()function since it is asynchronous (it's probably better to use the getJSON instead, since you are planning on sending back JSON). And you'll need to increment the index of 'sourceFileData' in the innerloop, instead of hard coding it to 0:

 $.getJSON("http://localhost:13839/MyWebApplication/SourceFile", function(sourceFileData) {

    var table = $("<table></table>");
    for (var i = 0; i < sourceFileData.length; i++) 
    {
        var line = $("<tr></tr>");
        for (var j = 0; j < sourceFileData[i].length; j++)
        {
            line.append( $(cell).html('<td> + sourceFileData[i][j] + '</td>'));
        }
        table.append(line);
    }

    table.appendTo( $("#sourceFileTable"));
});
cfs
  • 10,610
  • 3
  • 30
  • 43
  • My question is, I have my servlet returning a String[][]. Is var sourceFileData enough to assign a multi-dimensional array in javascript? Am I able to just traverse it like a regular multi-dimensional array? – Vernah Apr 17 '13 at 16:55
  • Ah yes, you're right about the inner loop, I missed that. I'm hesitant on using the Json library since my peers may not necessarily accept the use of too many external libraries. Is there another way beside Json? – Vernah Apr 17 '13 at 17:35
  • You could always roll your own, but you're going to have to define a data format to exchange between the server and client, be it JSON, XML, or something else. Most javascript libraries support JSON and XML, though JSON is more common. It will probably save you a lot of time and frustration to use an existing library; there's no sense in solving a problem that has already been solved. – cfs Apr 17 '13 at 17:46
  • I've updated the code in the OP with what I have now. I'm using Gson since it looks simple to use. However, I still can't get anything to display in the javascript. Does my servlet code look correct? – Vernah Apr 17 '13 at 18:16
  • At this point, you're diverging pretty far from the original question. It's probably best for you to do some debugging and ask a new question about either your servlet or front-end code. – cfs Apr 17 '13 at 18:27
  • Sorry about that. The servlet code I posted does indeed work, I'm now trying to get the javascript table making to work. For some reason, teh table.appendTo( $("sourceFileTable")); does not work. – Vernah Apr 17 '13 at 18:37