Reputation:
I've been reading a bit but can't find a simple answer to this. Say I have a HTML page, and some Javascript embeded in that page. Plus I have a java.class in a package called somePackage.someSubPackage.*;
How would I be able to invoke a method in java.class from whithin a javascript function in my HTML page?
Upvotes: 0
Views: 2606
Reputation: 168815
This works 'out of the box' for any browser with Java installed because the System
class is part of the J2SE.
<html>
<body>
<script type='text/javascript'>
document.write(java.lang.System.getProperty("java.version"));
</script>
</body>
</html>
To get it to work for custom classes (on the client side), it would usually require for those classes to be added to the class-path of a scriptable applet. Then use the JS/applet interface to invoke the methods & access the attributes.
Upvotes: 1
Reputation: 55897
Where is the Java going to run? It could be an Applet in the Browser or it could be running on a server somewhere.
JavaScript to Applet communication is possible, see, but most common these days is to use an AJAX call to a server.
You can wrap your Java as a callable service using several different techniques, I'd recommend using JAX/RS, it needs very little work. Your Java is then exposed as a callable URL that the JavaScript can invoke.
Hence you need to study two things:
Two separate problems, but both pretty easy.
Upvotes: 4
Reputation: 240860
Use DWR
DWR will generate the JavaScript to allow web browsers to securely call into Java code almost as if it was running locally. It can marshal virtually any data including collections, POJOs, XML and binary data like images and PDF files. All that is required is a security policy that defines what is allowed.
Upvotes: 1