可以啊,你在你的网页中将你的嵌入Applet java标上ID,
然后在vbscript 或者javascript中就可以访问java内的
公共方法,或者公共变量了。
然后在vbscript 或者javascript中就可以访问java内的
公共方法,或者公共变量了。
解决方案 »
- 关于Socket的,高手请进
- java的几个基本问题,继承,多态,pass by value 并散分
- 今天做的一个小题: 要用到两个变量 ,是俩日期:- 一个从数据库中读出来后, 月份和日子为单数是自动加零的。而另一个没有从数据库中读,月份单数不加零的,请问大虾们 java 中哪个类的方法可以转换它啊?。 感谢。。
- 怎么根据Class 生成该类的对象~
- 请问如何把从pl/sql中读出的OracleTypes.DATE型数据转换成String型“yyyy/MM/dd HH:mm”
- 伤心欲绝,再问一贴
- java 模拟登录网站
- 我要学JAVA
- 请问ASP如何和APPLET动态的传递变量?40分送上。
- 第一个告诉我并且正确的给100分
- 请大家介绍一下java BEAN的入门读物吧
- 我往数据库里写的中文为什么是乱吗呀?如何解决!
Call Javascript from a Java applet
Netscape only
A Javascript function is called from Java by using the showDocument method. A URL is needed with "javascript:" as the protocol.
[Java applet] import java.applet.*;
import java.net.*;public class inJava extends Applet{
public void init(){
String msg = "We will jump to an HTML tag";
try {
getAppletContext().showDocument
(new URL("javascript:doAlert(\"" + msg +"\")"));
getAppletContext().showDocument
(new URL("javascript:jumpTo(\"#JUMP\")"));
}
catch (MalformedURLException me) { }
}
}
[Javascript and HTML] <HTML><HEAD></HEAD><BODY>
<SCRIPT>
function doAlert(s) {
alert(s);
}
function jumpTo(tag) {
self.location=tag;
}
</SCRIPT>
<APPLET CODE="inJava.class"
NAME="myApplet" MAYSCRIPT
HEIGHT=400 WIDTH=10>
</APPLET>
<P>
<A NAME="JUMP">jump here from Java via Javascript</A>
</BODY>
</HTML>
Netscape AND Microsoft Internet Explorer v4
With IE4, Microsoft included the Netscape package netscape.javascript.* to allow a JAVA applet access to Javascript objects. To compile such Applet, you must adjust the CLASSPATH to include the Netscape java40.jar. To use the Netscape JAR, the CLASSPATH will look like this: set CLASSPATH=
.;c:\windev\jdk1.1.3\lib\classes.zip;
c:\Program Files\Netscape\Communicator\Program\Java\Classes\java40.jar;
Note that your CLASSPATH may be different depending on your JDK installation/version
To compile with IE, first locate the java\packages directory in the Windows or WinNt directory. You will find a bunch a zip files with strange names. On my installation (IE5), the file Qk1r3vrp.zip is the one containing the 2 classes you need, JSObject.class and JSException.Class. Include this zip in the classpath before compiling (or you can extract the 2 files (with directory) and create a new smaller ZIP (again with the directory) and use the new zip in the classpath). Note that if you compile with the Microsoft compiler, you don't need to do this. In the following example, you type in the TextField a Javascript function and press the button to execute the function. For example, try alert('Hello from JAVA'). Or you can execute function defined on the same page as the Applet. The Applet must contains the MAYSCRIPT parameter to be able to use JSObject. import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;public class jsJAVA extends Applet implements
ActionListener {
Button b;
TextField t;
public void init() {
t = new TextField(20);
add(t);
b = new Button("execute Javascript");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
JSObject win = (JSObject) JSObject.getWindow(this);
win.eval(t.getText());
}
}
}
Another way is to use the Reflection API. That way you don't need to modify your CLASSPATH for compilation or even import the netscape.jsobject package. // posted by C Werner on the realhowto list
import java.lang.reflect.*;
...
// Somewhere in the applet class ...
...
String jscmd = "window.close()"; /* JavaScript command */
String jsresult = null;
boolean success = false;
try {
Method getw = null, eval = null;
Object jswin = null;
Class c =
class.forName("netscape.javascript.JSObject"); /* does it in IE too */
Method ms[] = c.getMethods();
for (int i = 0; i < ms.length; i++) {
if (ms[i].getName().compareTo("getWindow") == 0)
getw = ms[i];
else if (ms[i].getName().compareTo("eval") == 0)
eval = ms[i];
}
}
Object a[] = new Object[1];
a[0] = this; /* this is the applet */
jswin = getw.invoke(c, a); /* this yields the JSObject */
a[0] = jscmd;
Object result = eval.invoke(jswin, a);
if (result instanceof String)
jsresult = (String) result;
else
jsresult = result.toString();
success = true;
}
catch (InvocationTargetException ite) {
jsresult = "" + ite.getTargetException();
}
catch (Exception e) {
jsresult = "" + e;
}if (success)
System.out.println("eval succeeded, result is " + jsresult);
else
System.out.println("eval failed with error " + jsresult);