import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JApplet;
import netscape.
javascript.
JSObject;
public
class AppletTest extends JApplet{
/**
*
*/
private static final long serialVersionUID = 2171316410722327511L;
public AppletTest() {
System.out.println("AppletTest============1");
}
public void init() {
System.out.println("AppletTest============2");
new Thread(){
public void run(){
try{
while(true){
Thread.sleep(1000);
System.out.println("init=======================" + getTime());
}
}
catch(Exception e){
e.printStackTrace();
}
}
}.start();
String parame = "{\"success\":true,\"info\":\"加载完成\"}";
System.out.println("加载完成=======================" + getTime());
runJS("initFinish", parame); //调用客户端js方法的
}
/**
*
* @param json 传入的字符串数据
* @param fun 回调的函数
*/
public void myMethod(final String json, final String fun){
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
new Thread(){ // 另起
线程,避免客户端等待applet返回数据卡死
public void run(){
try{
System.out.println("1Thread=======================" + getTime());
String parame = "{\"success\":true,\"info\":\"执行完毕\"}";
runJS(fun, parame); //调用客户端js方法的
}
catch(Exception e){
e.printStackTrace();
}
}
}.start();
System.out.println("2Thread=======================" + getTime());
return null;
}
});
}
public void runJS(final String jsFun, final String parame) {
new Thread(new Runnable() {
public void run() {
try {
String fun = "";
if(jsFun != null && !jsFun.equals("")){
fun = jsFun + "(" + parame + ")";
JSObject.getWindow(AppletTest.this).eval("javascript:" + fun + ";");
}
} catch (Throwable e) {
System.out.println("调用js出错=========" + e.getMessage());
}
}
}).start();
}
public void destroy() {
super.destroy();
System.out.println("================== destroy");
System.exit(0);
}
public static String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
}
html文件如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script>
var getTime =
function(formatStr, date){
formatStr = arguments[0] || "yyyy-MM-dd HH:mm:ss";
date = arguments[1] || new Date();
var str = formatStr;
var Week = ['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|YYYY/,date.getFullYear());
str=str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():'0' + (date.getYear() % 100));
str=str.replace(/MM/,date.getMonth()>9?(date.getMonth() + 1):'0' + (date.getMonth() + 1));
str=str.replace(/M/g,date.getMonth());
str=str.replace(/w|W/g,Week[date.getDay()]);
str=str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():'0' + date.getDate());
str=str.replace(/d|D/g,date.getDate());
str=str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():'0' + date.getHours());
str=str.replace(/h|H/g,date.getHours());
str=str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():'0' + date.getMinutes());
str=str.replace(/m/g,date.getMinutes());
str=str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():'0' + date.getSeconds());
str=str.replace(/s|S/g,date.getSeconds());
return str;
}
var appletObj = function(){
this.myMethod = function(json, fun){
try{
this.getInstance().myMethod(json, fun);
console.log(getTime() + "======myMethod:");
}
catch(e){
console.error(getTime() + "====applet程序挂了,请刷新页面:" + e);
}
}
var tool;
this.getInstance = function(){
console.log(getTime() + "======typeof tool:" + (typeof tool));
console.log(getTime() + "======tool:" + tool);
if(typeof tool == 'undefined'){
console.log(getTime() + "======getInstance:");
tool = $("#tool")[0];
}
return tool;
}
return this;
}();
$(function(){
$("#btn").click(function(){
var parame = {data1:'test1'};
appletObj.myMethod(
JSON.stringify(parame), "resultData");
});
});
function resultData(jsonObj){
console.log(getTime() + "========jsonObj:" + JSON.stringify(jsonObj));
console.log(getTime() + "======btn1Click:" + jsonObj.success);
}
function initFinish(jsonObj){
console.log(getTime() + "========applet init end:" + jsonObj.info);
}
</script>
</head>
<body>
<applet id="tool" code="AppletTest" codebase="." archive="AppletTest.jar" width="0" height="0" >
</applet>
<input type="button" id="btn" value="调用applet" />
</body>
</html>
/*
在applet调用js时出现
错误
java.lang.NoClassDefFoundError: com/sun/deploy/appcontext/AppContext
at fxApplet.pringString(fxApplet.java:33)
at fxApplet.init(fxApplet.java:21)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: com.sun.deploy.appcontext.AppContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 4 more
*/
解决办法,在jdk或者jre下面找到jar: deploy.jar,引入到项目下面就可以了。如果没有这个jar,建议你重新下载一个jre或者jdk.