Click to See Complete Forum and Search --> : Invocation of a static Java method from a Javascript function


olerag
07-03-2003, 11:41 AM
Can someone provide the javascript syntax for accessing a Java class from a javascript function. The static method can be proto-typed as "public boolean TestClass" and the javascript function can be called "TestIt()". No concern for agument values need apply.

Khalid Ali
07-03-2003, 11:52 AM
Take a look at this document
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/javobj.html#1193137

olerag
07-03-2003, 01:39 PM
Thanx Khalid,

The example in the link specifically referenced very simple class methods contained in the JDK library (such as String and Font). I'm trying to access a static method in my own class.

Is there some kind of configuration I need to perform so that Javascript can find the class (such as alteration of my classpath or, possibly, the preparation of "beans").

I don't believe this is a "JSObject" issue since I'm not trying to reference a javascript object from java. I simply want to call a previously compiled java class.

Khalid Ali
07-03-2003, 03:17 PM
direct access to a java class should work just like that

var obj packageName.className.methodName();

Bets policy in my opinion though, will be to use a java applet as a middle layer between your webpage and rest of the classes you want to use,that way you can access any class/method using a setter/getter method in the applet to invoke a prticular method in any other class..

here is an example of how web page javascript communication is done..

http://68.145.35.86/skills/javascripts/applets/AppletJavaScriptInteraction.html

Try this code with NS(prefereably)

olerag
07-03-2003, 10:47 PM
The syntax I tried in the js function was:

var jObj = new myClass.myStaticMethod();

When accessed on the browser, the error message stated that the class was not defined. The actual class resides in the same path as all of the other java servlet classes.

Note that when I tried this the following occurred:

var jObj = new java.lang.String("Howdy");
alert (jObj);

Netscape: Worked fine.
IE: Javascript error.

In your second example, the "new" keyword is not utilized. I haven't tried this yet but will give it a try when I return to work Monday.

Sorry, but I'll be staying away from applets. Tired of getting different results from browsers unless the JRE plug-in is used. However, this "bear" seems to have a new version every Tues (like, "whimpy") and our webmaster is getting a little muddled especially after RMI was introduced.

Anyway, I think I'll just use an intermediate servlet class to interact with the class/method I need to access.

Thanks for your time and help.

Khalid Ali
07-03-2003, 10:50 PM
good choice (I love servlets),
using java classes could be messy as well as it will not work on IE anyways...applets is the next best choice if you want direct interaction with your java objects.