1

I have an array of user defined objects which I'm passing from a servlet to a JSP. Now, I need to access this array within the script tag in my JSP. How do I do it?

For now, I have the following within script tag:

var dCount = '${chart}';

var index;
   for(index=0; index<dCount.length;index++){
        alert(dCount);
    }

When I do this, I get the output as undefined. I assume this is bcause the ${chart} returns an Object? If so, how do I convert it to my user defined class? Or is something else the problem here?

Thanks.

2 Answers 2

0

You need to realize that JSP is basically a HTML code generator and that CSS/JS is part of the generated HTML output. You need to write JSP code in such way that valid HTML/JS code is generated. A valid JS array of 3 objects with each the properties foo and bar look like this:

var chart = [{
    foo: "foo1",
    bar: "bar1"
},{
    foo: "foo2",
    bar: "bar2"
},{
    foo: "foo3",
    bar: "bar3"
}];

var index;
for(index = 0; index < chart.length; index++) {
    var item = chart[index];
    alert(item.foo + ", " + item.bar);
}

Now, you need to make sure that you write JSP code in such way that exactly this valid HTML/JS code is generated (as you can verify by rightclick and View Source in webbrowser).

Provided that ${chart} is a List<Item> or Item[], one of most naive ways is using <c:forEach> to loop over it, printing the desired JS code output:

var chart = [<c:forEach items="${chart}" var="item" varStatus="loop">
    { foo: "${item.foo}", bar: "${item.bar}" }${loop.last ? '' : ','}
</c:forEach>];

In its simplest form, it should work fine. However, trouble starts once you get "special characters" as property values, such as " or even a newline. You'd need to escape it. Much better is to use an existing JSON library, such as Google Gson in order to convert a complex Java object, such as a List<Item> or Item[], to a valid JSON string. E.g. as follows in the servlet:

List<Item> chart = createItSomehow();
request.setAttribute("chart", new Gson().toJson(chart));
// ...

Then you can just do:

var chart = ${chart};
Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, but if I convert the array of objects into JSON string, how will I call the methods of the object from the script? For instance, I need to call a method like: dCount.getCount(); or dCount.getName().
Just the usual JS object way by item.count, item.name, etc, exactly as demonstrated in my answer.
Thanks BalusC, works like a charm. I just had to add var chart = JSON.parse('${chart}');
That shouldn't be necessary if you just do var chart = ${chart}; exactly as shown in my answer (yes, without quotes!). When you print the JSON object in quotes, it becomes a JSON string which you obviously need to parse in order to get a JSON object. This forth-and-back massaging doesn't make sense. Just let JSP/EL print it directly as if it's a JSON object. Read the first paragraph of the answer once again until you get the adrenaline rush which you would get when you start to finally understand something.
0

Java script is executed on client side and el is evaluated on server side .I think alternately you can make an ajax request in java script to server and can return the user list as string becuase in java script you can only use string and not any other data type .

Comments

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.