通过JavaScript小应用程序查杀 [英] Killing applet through javascript

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

问题描述

我创建了具有必要执行下列code的小程序:

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()" />  

问题:在点击<大骨节病>开始我的浏览器停止响应,并且在JavaScript的code没有错误也是如此。有什么办法来调用 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?

推荐答案

这是唯一的转弯code到 SSCCE 1 的许多问题变得清晰:

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


  • 这code失败,我没有JS的迹象。这表明,我认为这有没有与JS做的!

  • 示例的实例上初始化()被称为是不一样的,因为这其中是小程序!如果UI曾经检测到的JS这并不重要,它不会停止正在运行的实例。

  • 接受()挡住了EDT。

  • 设置关闭真正是不是有一个有效,直到连接下一个客户端和$ C后$ C绕一圈检查的值关闭属性一次。我通过调用来实现它 ss.close()(这使得关闭属性冗余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 (你应该用自己的code折腾JS到混合之前已经选中)。在源顶部的单行注释所使用的AppletVewer把code屏幕。使用这样的:

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天全站免登陆