admin管理员组

文章数量:1415476

I am working on a project where I access the APIs and get the output using Java. Now I want to display the data using some graphs and other data visualization tools in a browser. I have searched for some JavaScript libraries for that. But how can I connect java and javascript to get the output in a browser.

Can I do it with JSP? I don't want to use applets. Please suggest other ways also.

EDIT: If I use JSP then I will have to host a server. Is there a direct way without hosting a server?

I am working on a project where I access the APIs and get the output using Java. Now I want to display the data using some graphs and other data visualization tools in a browser. I have searched for some JavaScript libraries for that. But how can I connect java and javascript to get the output in a browser.

Can I do it with JSP? I don't want to use applets. Please suggest other ways also.

EDIT: If I use JSP then I will have to host a server. Is there a direct way without hosting a server?

Share Improve this question edited Jan 24, 2013 at 9:05 dejavu asked Jan 23, 2013 at 5:48 dejavudejavu 3,2547 gold badges38 silver badges60 bronze badges 5
  • 1 Any reason why you can't use pure Javascript? – Makoto Commented Jan 23, 2013 at 5:51
  • Am using JAVA to get data from JIRA ( atlassian./software/jira/overview). Jira Java Rest Client makes it easy to get data using JAVA. So I preferred Java – dejavu Commented Jan 23, 2013 at 5:53
  • 2 You can easily access the JIRA REST API with pure JavaScript. – Perception Commented Jan 23, 2013 at 5:55
  • Java is not an acronym. No need to capitalize. – tucuxi Commented Jan 23, 2013 at 5:55
  • Initially I thought of using JavaScript, but couldn't find some starting tutorial so I continued it with Java. Seems like I have to rewrite the whole thing in Javascript. – dejavu Commented Jan 23, 2013 at 5:56
Add a ment  | 

4 Answers 4

Reset to default 3

Yes you can do that with JSP(java server page). By passing data from Java to javascript in your jsp page. JSP page typically piled into raw html and send it to client.

Simple example of passing value java to javascript in jsp page.

<%
  String str="Hello world";
%>
<script>
 var jsvar = <%=str%>;
</script>

A example of java list values to js variable using jsp scriptlet.

var  myArray = new Array();
<% for (int i=0; i<list.size(); i++) { %>
      myArray[<%= i %>] = "<%= list.get(i) %>"; 
<% } %>

Better use jstl tag library which is easier to write and structured.

JSP documentation JSP library JSTL documentation 1 JSTL library

Have Java generate an .html file; open it in the browser.

You can open the default system browser on a webpage using Desktop.open(). It allows you to open any file with the default application associated with its file type on the system.

The .html file would have the java-generated data, and can reference JS libraries in the web or in the local filesystem. You can even add a refresh button (or timeout) to check for updated information.

Of course, JSPs or other technologies would work; but those require a server running your application.

you can use Direct Web Remoting(DWR). DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.

DWR is Easy Ajax for Java

It consists of two main parts:

Code to allow JavaScript to retrieve data from a servlet-based web server using Ajax principles.
A JavaScript library that makes it easier for the web site developer to dynamically update the web page with the retrieved data.

check out the link-

[DWR][1]

And demo example is given here- DWR chat example

HTML is the place to start. JSP can help you generate HTML. Find how to dynamically create images from your data with Java. If that is not enough, read about JSON, Ajax, jquery, and add some JavaScript.

本文标签: Connect java with javascript for data visualization in BrowserStack Overflow