I have created an applet which has a necessity to execute the following code:
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
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 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?
It was only after turning that code into an SSCCE1 that the many problems became clear:
Exampleon whichinitialize()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.accept()was blocking the EDT.closedtotruewas not about to have an effect until after the next client connected and the code looped around to check the value of theclosedattribute again. I achieved it by callingss.close()(which makes theclosedattribute redundant BTW – but I left it in).BTW
Code
Try this version:
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:
Typical Output