50645

Killing applet through javascript

Question:

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

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

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

Problem: On clicking <kbd>Go</kbd> 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?

Answer1:

It was only after turning that code into an <a href="http://sscce.org/" rel="nofollow">SSCCE</a><sup>1</sup> that the many problems became clear:

<ul><li>This code failed for me without any sign of JS. That indicates to me that this has <strong><em>nothing</em></strong> to do with JS!</li> <li>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.</li> <li>The accept() was blocking the EDT.</li> <li>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).</li> </ul><h3>BTW</h3> <ol><li>Please consider posting an SSCCE in future.</li> </ol><h2>Code</h2>

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

I have added a button to this version so that you can check it works as expected <strong>without JavaScript</strong> (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 <h2>Typical Output</h2> 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

Answer2:

I don't believe that the applet security sandbox allows you to invoke a System#exit(). Even when it did, it would be bad for user experience as the enduser wouldn't be able to run it again without reopening the page in browser.

Rather let that while intercept on a boolean instance variable which you toggle in the close() method. E.g.

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

Answer3:

try this from <a href="http://pastebin.com/dhqX7SEt" rel="nofollow">pastebin.com</a> and <a href="https://stackoverflow.com/questions/1267033/kill-java-applet-via-javascript" rel="nofollow">kill-java-applet-via-javascript</a>

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

Recommend

  • Linear gradient not applying in Webkit with d3 generated SVG
  • How to change default stop edit behavior in jtable
  • What is this strange character in chrome's resource css viewer?
  • Send data from edittext to listview
  • Hide buttons on title bar in Java
  • Time out Error in send mail
  • Jquery Mobile pageLoading() Method how does it work?
  • what makes a request a new request in asp.net C#
  • Simulate click Geckofx vb,net
  • System.InvalidCastException: Specified cast is not valid
  • Keep this build forever option - Jenkins
  • Read a local file using javascript
  • Android fill_parent issue
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Join two tables and save into third-sql
  • Trying to switch camera back to front but getting exception
  • How to model a transition system with SPIN
  • what is the difference between the asp.net mvc application and asp.net web application
  • To display the title for the current loaction in map in iphone
  • ORA-29908: missing primary invocation for ancillary operator
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Matrix multiplication with MKL
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Programmatically clearing map cache
  • Busy indicator not showing up in wpf window [duplicate]
  • Binding checkboxes to object values in AngularJs
  • failed to connect to specific WiFi in android programmatically
  • Converting MP3 duration time
  • Why do underscore prefixed variables exist?