I was wondering how to import my java packages into a js file and then call class methods on created object.
The reason I need to import is I wish to get data from a connection to my database, regarding dates and I have a class that lets me do so. So I need to import the Package, then create a class object, then call methods on my object.
I would appreciate your help, Thank you, John
I was wondering how to import my java packages into a js file and then call class methods on created object.
The reason I need to import is I wish to get data from a connection to my database, regarding dates and I have a class that lets me do so. So I need to import the Package, then create a class object, then call methods on my object.
I would appreciate your help, Thank you, John
Share Improve this question edited Sep 22, 2010 at 9:58 John asked Sep 22, 2010 at 9:51 JohnJohn 691 gold badge1 silver badge2 bronze badges 4 |6 Answers
Reset to default 8The questions does not specify that this is a browser-based question. Java ships with Rhino since version 6. So, on the off chance that someone wants to know how to do this - e.g., using JavaScript as a scripting language for 3rd party tools like soapUI or PTC Integrity - it's incredibly simple.
importPackage(java.io);
Check out Wikipedia:Rhino (JavaScript engine) as a good starting point.
You can use JSP and use Java basic types as parameter for your javascript.
Import your Java classes in JSP, e.g:
<%@page import="com.acme.MyClass"%>
Then in your JS which also in the JSP:
<script type="text/javascript">
var f = function(param){
console.log(param);
}
f('<%= MyClass.foo().toString() %>');
</script>
Would that be something you're looking at?
Hope that helps.
I think solution for you problem is DWR technology. As I guessed you think that it is too easy to invoke Java methods(Server side) from JavaScript(Client side). DWR is exactly what you are looking for :)
You'll have to store the Java as a Java server page on your server to do the connection, and feed javascript the database data via ajax, because javascript is incapable of connecting to any protocols other than http, file and ftp.
Actually you CAN do this. You can create a Java Applet with public methods. Once you add this to your page you can access these methods via JavaScript. I'll see if I can dig up a reference for you.
Here's one link!
I've done this myself, years ago. It works well. I don't know if I would do it again for the reason you specify, but it is possible.
Also in addition to this method and the many other fine suggestions, have a look at GWT. http://code.google.com/webtoolkit/overview.html
Calling java methods from javascript is not possible.
JSP file:
function doSomething {
<% SomeClass.someMethod(); %>
}
<%!
class SomeClass {
public static void someMethod() {
//
}
}
%>
someMethod will not run on invocation of doSomething but it will run when jsp generates response.
java != javascript
REMEMBER IT! – Randy the Dev Commented Sep 22, 2010 at 9:53java
is tojavascript
whatham
is tohamsters
;) – tim_yates Commented Sep 22, 2010 at 10:00