I'm a beginner with javascript, and I don't know how to program in java, and trying to understand servlets has been really hard. All I understand is how to create a jsp page which submits a complex query to my Postgres 9.2 database and returns the result rows to html which I then display in an iframe on the client side. What I want to do is take the same result rows in that same jsp page and also send them to an array on the client side javascript so I can chart the data in flot. If I can get this to work maybe that would save me the headache of trying to understand java and serlets, and would make my plotting easier since the graph would plot exactly what the query returns every time the query changes. I don't want to query the database more the necessary. I've read here before that using jsp for things other than printing html is not recommended, but according to this post Populating JavaScript Array from JSP List I may be able to print the result rows to an array and have that available to the javascript on the client side. However I can't get the client side to see the array variable, and I'm sure I've got something missing and don't understand something fundamental.
The rows I print out from my query result using "c:forEach" and "c:out" into html looks like this:
"Jan" 1 1426.50 472.65
"Feb" 2 1449.00 482.10
"Mar" 3 1459.50 485.55
"Apr" 4 1470.00 489.00
"May" 5 1480.50 492.45
"Jun" 6 1491.00 495.90
"Jul" 7 1489.50 493.35
"Aug" 8 1512.00 502.80
"Sep" 9 1510.50 500.25
"Oct" 10 1533.00 509.70
"Nov" 11 1543.50 513.15
"Dec" 12 1542.00 510.60
From what I understand in that link, I should be able to make an array look like this:
var withdraw_v = [[ 1.0, 1426.50],[ 2.0, 1449.00],[ 3.0, 1459.50],[ 4.0, 1470.00],[ 5.0, 1480.50],[ 6.0, 1491.00],[ 7.0, 1489.50],[ 8.0, 1512.00],[ 9.0, 1510.50],[ 10.0, 1533.00],[ 11.0, 1543.50],[ 12.0, 1542.00]];
using this code in my jsp file:
<sql:query var="rs" dataSource="jdbc/medford">
My sql query goes here
</sql:query>
<script type="text/javascript">
var withdraw_v = [
<c:forEach var="row" items="${rs.rows}" varStatus="status">
[ <c:out value="${row.Month_num}"/>, <c:out value="${row.Average_Withdrawals}"/>]
<c:if test="${not status.last}">,</c:if>
</c:forEach>
];
</script>
In my html/javascript file, I try to plot my data using the array:
$.plot("#plot", withdraw_v);
But it returns that the withdraw_v variable is undefined. I'm sure I've got this all wrong. Can anyone point out what might make this work? I've tried reading about google-gson, but I'm not understanding it either, or whether it would help me in this instance.