2

I'm familiar with a variety of programming languages, my best being java. I'm in the process of learning javascript, and there are a ton of resources out there for that. But I'm struggling to find resources on how to connect to java code which would then return results.

As an example, I can write java programs to pull data from a database and manipulate/aggregate it. Say I want to build a page which gives the users a few options on how to pull/aggregate the data. I can easily write some java code to do this, and with some googling I should be able to easily build a web page which gives the users some options. What I don't know how to do is how to have javascript execute the java code (passing arguments) and get the data to display from java. From reading it seems like the best format for passing the data would be JSON and I understand the basics of that but my issue is figuring out how to call a java program from javascript in real time and how to pass the JSON back to javascript to use on the page.

I realize it's a really general question, I'm not looking for someone to explain this but more links to references that would help me get started with this. Thanks!

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
zachvac
  • 650
  • 1
  • 6
  • 17
  • If you're looking for only displaying the data, JSP works very well with Java classes. If you need a more interactive page, then you can work with javascript and ajax for page to server (your java code exposed as a URL) communication. – Abhishek Jul 05 '16 at 22:43

3 Answers3

5

JavaScript runs sand-boxed within the browser and cannot really call modules written in other languages (because those won't be sand-boxed with it).

This is in fact how most interactive sites work: Server modules are written in whatever works for you (Java, C# MVC, PHP, even JavaScript) and called from JavaScript in the browser using AJAX calls.

Cobus Kruger
  • 8,338
  • 3
  • 61
  • 106
1

You can use Restful Web Service as backend in whatever language or framework like in your case you can write restful service using Spring and then get and update data using standard http protocol like GET, POST etc.

For backend code, here is a link.

https://spring.io/guides/gs/rest-service/

https://spring.io/understanding/REST

In front-end code you can use any library or framework. Possible options are jQuery, AngularJs, Backbone etc. If you are new to front end technologies, jQuery is simpler option.

Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
0

Fair warning, I'm not a Java guy, but the fundamentals should be the same regardless of the language. What you're looking for is AJAX. AJAX allows you to talk to the server from the browser and receive back data. Without know exactly what you're trying to do I can't help too much but you should be able to get going in the right direction.

Basically something like this:

jQuery:

$.ajax({
  url: "test.java", // your java file here
  context: document.body,
  dataType: "json", // make sure it's you're using JSON
}).done(function() {
  $( this ).addClass( "done" ); 
});

Javascript:

Rather than giving you a hack answer, check this Stack post out instead. Lots of great info:

How to make an AJAX call without jQuery?

Using AJAX with Java:

This might have some more info on your specific use case:

https://netbeans.org/kb/docs/web/ajax-quickstart.html

Hope that points you in the right direction.

Community
  • 1
  • 1
Michael Heath
  • 174
  • 2
  • 11