Killing an applet via javascript

I created an applet that needs to execute the following code:

the 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

A fragment of the HTML file for executing the 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()" /> 

Problem: when I click Go, my browser stops responding, and there is no error in javascript either. Is there a way to call the document.app.closed(); method document.app.closed(); ?

+1
source share
3 answers

Only after this code turned into SSCCE 1 did many problems become clear:

  • This code failed for me without any JS signs. This tells me that it has nothing for JS!
  • The Example instance that initialize() was called on was not the same as the one that was the applet! It doesn't matter if the user interface was ever detected by JS, it would not stop the executable instance.
  • accept() blocked EDT.
  • Setting closed to true should not have an effect until the next client connects and the code loops to check the value of the closed attribute again. I achieved this by calling ss.close() (which makes the closed attribute redundant BTW - but I left it).

BTW

  • Please consider publishing SSCCE in the future.

the code

Try this version:

 // <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"); } } } 

Run

I added a button for this version so you can test it as expected without JavaScript (which you should have checked with your own code before throwing JS into the mix). An individual line comment at the top of the source is used by AppletVewer to place the code on the screen. Use it like this:

 prompt> appletviewer Example.java 

Typical output

 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 
+2
source

I do not believe that the applet sandbox allows you to call System#exit() . Even when this was done, it would be bad for the user, since the end user could not start it again without opening the page in the browser.

Rather, let this while intercept the boolean instance variable that you switch in the close() method. For instance.

 private boolean closed; public void initialize() { while (!closed) { // ... } } public void close() { this.closed = true; } 
+1
source

try this from pastebin.com and kill-java-applet-via-javascript

 <script> document.MyApplet.killApplet(); </script> public void killApplet() { AccessController.doPrivileged(new PrivilegedAction() { public Void run() { // kill the JVM System.exit(0); return null; } }); } 
0
source

Source: https://habr.com/ru/post/1415214/


All Articles