Reputation: 463
Currently, I have a servlet that forwards to a jsp. The jsp has access to a session attribute "current". During the loading of the jsp, the information in "current" is passed to a javascript function that generates a graph. This is all working fine. My only problem is that i'm hard coding the graph data.
How would i go about passing the data array from the servlet to the jsp. Basically, in the creerRapport function, in the 5th argument, how do I replace that with java attributes?
Any help or ideas would be appreciated.
My current code with hard coded data.
<body onload="soumettreRapport();">
<script type="text/javascript">
function soumettreRapport() {
creerRapport( "${current.title}",
"${current.type}",
${current.width},
${current.height},
[
{
key: "Cumulative Return",
values: [
{
"label" : "2001" ,
"value" : -29.76
} ,
{
"label" : "2002" ,
"value" : 0
} ,
{
"label" : "2003 ,
"value" : 32.80
}
]
}
]
);
return false;
}
Upvotes: 0
Views: 370
Reputation: 9655
In Servlet, You need to to have JSON array as String then put this String into Request scope.
String jsonArrayString = convert(...); // Output [{key:"Cumulative Return", .... }]
request.setAttribute("jsonArrayString", jsonArrayString);
In JSP:
function soumettreRapport() {
var jsonArray = ${jsonArrayString};
creerRapport( "${current.title}",
"${current.type}",
${current.width},
${current.height}, jsonArray );
}
Upvotes: 1