Connect java to javascript to render data in the browser

I am working on a project where I am accessing the API and getting the output using Java

. Now I want to display data with some graphs and other data visualization tools in the browser. I have searched for some libraries JavaScript

for this. But how can I connect java and javascript to get the output in the browser.

Can I do it with JSP

? I don't want to use applets. Also suggest other ways.

EDIT: if i use JSP i have to host the server. Is there a direct way without hosting a server?

+3


source to share


4 answers


Yes, you can do it with JSP(java server page)

. By passing data from Java to javascript in a jsp page. The JSP page is usually compiled to raw html and sends it to the client.

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

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

      

Example java list values ​​for js variable using jsp scriptlet.



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

      

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

JSP Documentation JSP Library JSTL Documentation 1 JSTL Library

+2


source


Ask Java to generate the .html file; open it in your browser.

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



The .html file will have java generated data and can link to JS libraries on the internet or on the local file system. You can even add a refresh button (or timeout) to check for updated information.

Of course JSP or other technologies will work; but they require a server that is running your application.

0


source


you can use Direct Web Remoting (DWR). DWR is a Java library that allows Java on the server and JavaScript on the browser to communicate and name each other as easily as possible.

DWR - Simple Ajax for Java

It has 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 link -

[DWR][1]

      

Here's a sample demo - DWR Chat Example

0


source


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

0


source







All Articles