通过javascript杀死小程序 [英] Killing applet through javascript

查看:35
本文介绍了通过javascript杀死小程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小程序,它需要执行以下代码:

I have created an applet which has a necessity to execute the following code:

public class Example extends JApplet {
private ServerSocket ss ;
private Socket socket;
private boolean closed;

@Override
public void init(){
    try {
        new Example().initialize();
    } catch (IOException ex) {
        Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null,ex);
    }
}
public void closed(){
    System.out.println("Inside close");
    this.closed=true;
  }
public void initialize() throws IOException{
    ss =new ServerSocket(5002);
    while(!closed){
    System.out.println("Waiting to accept request");
    socket = ss.accept();
    System.out.println("Request accepted");
    } 
}}

HTML

执行小程序的 HTML 文件片段:

HTML

Fragment of HTML file to execute applet:

<script type="text/javascript" >  
 function closeCall(){  
 document.app.closed();  
 }  
 </script>  
   <body>  
<applet id="app" code="example.Example" archive="Example.jar" height="300" width="300">    
   </applet>  
   <input type="button" value="go" onClick="closeCall()" />  

问题:在单击 Go 时,我的浏览器停止响应,并且 javascript 代码中也没有错误.有没有办法调用document.app.closed();方法?

Problem: On clicking Go my browser stops responding and there is no error in javascript code as well. Is there any way to call the document.app.closed(); method?

推荐答案

只有在将该代码转换为 SSCCE1许多问题变得清晰:

It was only after turning that code into an SSCCE1 that the many problems became clear:

  • 这段代码对我来说失败了,没有任何 JS 的迹象.这向我表明这与 JS 没有有关!
  • 调用initialize()Example 的实例与小程序的实例不一样!UI 是否检测到 JS 并不重要,它不会停止正在运行的实例.
  • accept() 阻止了 EDT.
  • closed 设置为 true 直到下一个客户端连接并且代码循环检查 closed<的值后才会生效./code> 属性.我通过调用 ss.close() 实现了它(这使得 closed 属性变得多余 BTW - 但我把它留在了).
  • This code failed for me without any sign of JS. That indicates to me that this has nothing to do with JS!
  • The instance of Example on which initialize() was called was not the same as that which was the applet! It did not matter if the UI ever detected the JS, it would not have stopped the running instance.
  • The accept() was blocking the EDT.
  • Setting closed to true was not about to have an effect until after the next client connected and the code looped around to check the value of the closed attribute again. I achieved it by calling ss.close() (which makes the closed attribute redundant BTW - but I left it in).
  1. 请考虑在未来发布 SSCCE.

代码

试试这个版本:

// <applet code='Example' width=400 height=100></applet>
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.logging.*;

public class Example extends JApplet {
    private ServerSocket ss ;
    private Socket socket;
    private boolean closed;

    @Override
    public void init(){
        JButton stop = new JButton("Stop!");
        stop.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                closed();
            }
        });
        add( stop );
        validate();

        Runnable r = new Runnable() {
            public void run() {
                try {
                    initialize();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        };
        Thread t = new Thread(r);
        t.start();
    }

    public void closed() {
        System.out.println("Inside close");
        closed=true;
        try {
            ss.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void initialize() throws IOException {
        ss =new ServerSocket(5002);
        while(!closed){
            System.out.println("Waiting to accept request");
            socket = ss.accept();
            System.out.println("Request accepted");
        }
    }
}

运行

我在此版本中添加了一个按钮,以便您可以在没有 JavaScript 的情况下检查它是否按预期工作(在将 JS 加入混合之前,您应该使用自己的代码进行检查).AppletVewer 使用源代码顶部的单行注释将代码显示在屏幕上.像这样使用它:

Run

I have added a button to this version so that you can check it works as expected without JavaScript (which you should have checked with your own code before tossing JS into the mix). The single line comment at the top of the source is used by AppletVewer to put the code on-screen. Use it like this:

prompt> appletviewer Example.java

典型输出

Waiting to accept request
Inside close
java.net.SocketException: socket closed
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
    at java.net.ServerSocket.implAccept(ServerSocket.java:462)
    at java.net.ServerSocket.accept(ServerSocket.java:430)
    at Example.initialize(Example.java:51)
    at Example$2.run(Example.java:27)
    at java.lang.Thread.run(Thread.java:662)

Tool completed successfully

这篇关于通过javascript杀死小程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆