Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:56:12+00:00 2026-05-31T14:56:12+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:56:13+00:00Added an answer on May 31, 2026 at 2:56 pm

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

    • 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).

    BTW

    1. Please consider posting an SSCCE in future.

    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 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
    

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created an applet which has one large panel to display data surrounded by
I have the following problem: I have a Wintec WBT-202 GPS device which has
I have created a UITableViewCell using UITableViewCellStyleValue1, which the Apple docs define as: A
I have created a voice recorder applet. The output sound file will be .wav
Hi I have created a Self Signed Applet , but not able to access
I have created a UserControl that has a ListView in it. The ListView is
I have created a C# class file by using a XSD-file as an input.
i have a tableview which has image and a text behind, i create the
I have a model named UserPrice which has the attribute :purchase_date (a date_select) in
I have created a custom content type that has a ReferenceField. When I set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.