-1

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.

Community
  • 1
  • 1
Jan
  • 101
  • 2
  • View the HTML source. See if there are any errors in the way the Javascript was written by the JSP, and correct. You can also use Firefox and Tools->Error Console to Javascript runtime type errors. – developerwjk Sep 23 '13 at 17:07
  • The preamble, justification, etc. isn't really necessary. Please, just ask your question; it's OK if it's a "beginner" question, just get to it and ask. Only add the information that we need to understand what you're asking, and what we need to do to help. – Bob Gilmore Sep 23 '13 at 17:10
  • Firebug output shows the response from the Net->html as: – Jan Sep 23 '13 at 18:24
  • The only other error is: "ReferenceError: withdraw_v is not defined" – Jan Sep 23 '13 at 18:35
  • Are you referencing withdraw_v before you define it perhaps? – developerwjk Sep 23 '13 at 18:53
  • I've now defined it on the html page, but when I log the value of withdraw_v to the console, it just shows as an empty array "[]", so does that mean it's not being passed from the jsp back to the client side? I'm so confused whether this strategy should even work. – Jan Sep 23 '13 at 19:58
  • If you are wandering whether the values made it from the serverside to the client or not there is one way to find out: view source in the browser. That needs to be your first instinct. – developerwjk Sep 23 '13 at 23:07

1 Answers1

0

I solved my problem by realizing if you have a starting html page which calls a jsp page, and that jsp page queries the database and returns a result set, you can't then send those results in some form back to the original html page to interact with javascript on that original html page.

Instead I used the html page to call a jsp page which then returns the database results into a new html page which is inserted into an iframe on the original page. The flot chart was correctly drawn in the iframe.

Jan
  • 101
  • 2