views:

138

answers:

4

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?

A: 

I've been there. See http://www.apl.jhu.edu/~hall/java/Java-from-JavaScript.html

Midhat
A: 

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>
pramodc84
I think we need to specify the Class Name with this
harigm
Yes corrected mistake
pramodc84
although It calls scriplet(<% %>) code first whenever i load the page.
saloni
@saloni Yes you are right it will call on load and only once. we can't interact between JS and Java directly
pramodc84
@pramodc84 - your answer is premised on the assumption that there is no client-side (i.e. browser resident) Java. This is not necessarily true.
Stephen C
+2  A: 

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.

Daniel Earwicker
+4  A: 

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

Examples

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.

Narayan
'without AJAX' ? I'm not sure about the OP's motivation not to use JS, so you may want to address that.
Brian Agnew