在JRE1.4.2以上版本中解决Javascript调用已签名Applet方法时提示没有权限问题

来源:互联网 发布:轩辕剑重剑进阶数据 编辑:程序博客网 时间:2024/04/27 22:06

在JRE1.4.2以上版本中,如果是javascript掉用Applet方法时,即使用户已经信任了这个Applet的签名,还是有可能会出现权限不够的问题,但是如果Applet自己调用方法则没有问题,为此,可以在Applet中的start方法中启动一个线程,这个线程有足够的权限,然后在javascript方法中和这个线程进行交互,以下是代码

public class MyApplet extends Applet {
  Thread thread;
  String command;
  Object result;
  boolean isEnd = false;

  public void start(){
    thread = new Thread(new Runnable() {
      public void run(){
        while(!isEnd){
          try{
            synchronized(thread){
              thread.wait();
            }
          }catch(Exception e){e.printStackTrace();}
          if(isEnd) return;
          try{
            if(“CallExe“.equals(command))
                result = callExe();
          }catch(Exception e){e.printStackTrace();}
          synchronized(CardApplet.this){
            CardApplet.this.notify();
          }
        }
      }
    });
    thread.start();
  }

  public void stop(){
    isEnd = true;
    synchronized(thread){
      thread.notify();
    }
  }

  String call(){
    this.command = “CallExe“;
    try{
      synchronized(thread){
        thread.notify();
      }
      synchronized(this){
        wait();
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    return result;
  }

String callExe(){
    //todo add logic here
    return null;
  }

}