Reputation: 173
In my Java Script , I want to call a java method . Is it possible to call a java method by js w/o using ajax?
Upvotes: 1
Views: 1462
Reputation: 1614
You can't directly call Java method in javascript irrespective of Ajax present or not. Javascript is at client side.
If you are using JSP, then you can call using scriplets
<script>
....
if(somecondition) {
<%
ClassXXX.xXXX();
%>
}
....
</script>
Upvotes: 0
Reputation: 6261
use Direct Web Remoting for your Javascript to Java Interaction
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
EDIT: as Daniel said u can use Rhino interpreter or run an applet to do the call
If you're running from a browser then there is a sandbox which has a security policy that defines and disallows calling Java Methods directly from browser.
Upvotes: 4
Reputation: 116744
If you are running in Rhino javascript, which is a JS engine implemented in the JVM, then yes.
If you're running a browser, the answer is almost certainly no, at least directly. You could write a Java applet to run in the browser, and you might be able to call methods in it from JavaScript.
Upvotes: 2