2

I am using jsp for scripting. I have a page where I collect data from dropdown boxes and radio buttons and then form a query based on data inserted by the user.

I then hit the url(query here is append to a url i.e a query string) and in return I get an xml file.

I parse the file and formulate it in a particular way in a string which I need to pass it to a javascript function, that renders the graph based on passed values.

I wanted to know how/where should I call the javascript function and pass the JSP string to it so that it can render the graph.

I have tried putting it on OnClick event in the html form but I am not sure if it's the correct way of doing it. Please let me know how can I go about this porblem.

Form

1 Answer 1

6

JSP cannot call JavaScript functions. JSP is a server side view technology which runs in the webserver and generates HTML/CSS/JS output. JavaScript is a client side scripting language which runs in webbrowser and works on HTML DOM tree.

You can however let JSP print JavaScript code accordingly so that it get executed in the webbrowser once the HTTP response arrives. E.g.

<script>someFunction('${someString}');</script>

Imagine that ${someString} resolves to a string "foo", then when JSP runs, the webbrowser will retrieve the following:

<script>someFunction('foo');</script>

(rightclick page in browser and do View Source to see it yourself)

Once the webbrowser gets to that line, it will then execute the JS function with the string variable which is printed by JSP.

See also:

Sign up to request clarification or add additional context in comments.

3 Comments

Okay thanks for a quick reply. I just tried calling the drawGraph(data) function which is a javascript function by printing it through jsp. Now after I have all the data formulated in my data variable I am calling the function this way out.println("<script>drawGraph("+data+")</script>"); And my drawGraph function puts the graph based on div tag so below this jsp script i have normal html as following <div id = "right"> <div id="chartdiv" style="height:400px;width:300px;"></div> </div>
Actually I am not really familiar with jsp. Right now I want something working. I have been having hard time figuring out things. What method do you suggest to be the best any tutorial on this?
I suggest to follow the links and not to ignore them. I do not post them for decoration :)

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.