I found an application that gets the source from a webpage. That worked fine. I tried converting it to an applet, and it works great in netbeans. The problem is when I try to run it in a browser, it does nothing. As you can see from the code, I tried to add a printwriter so I can just open the file and read what it got, but the applet doesn't even load that way. I read up on that and apparently I have to get the applet signed, and I have no idea what that is.

So then I tried to just get something printed out to a textbox. The applet loads fine, but when I hit the start button, it does nothing. Any ideas?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.HttpURLConnection;
import java.net.URL;
import java.applet.*;
import javax.swing.*;
import java.io.*;

public class main extends Applet{
  JButton go = new JButton("Start!");
  private String source;
  JTextArea codeArea = new JTextArea(200,200);

  //File outFile = new File("test.txt");
  //FileOutputStream outFileStream = new FileOutputStream(outFile);
  //PrintWriter outStream = new PrintWriter(outFileStream);

  public main() throws IOException{

    add(go);
    add(codeArea);
    go.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          URL pageURL = new URL("www.google.com");
          HttpURLConnection urlConnection = (HttpURLConnection) pageURL.openConnection();
          int respCode = urlConnection.getResponseCode();
          String response = urlConnection.getResponseMessage();
          //codeArea.setText("HTTP/1.x " + respCode + " " + response + "\n");
          int count = 1;
          while (true) {
            String header = urlConnection.getHeaderField(count);
            String key = urlConnection.getHeaderFieldKey(count);
            if (header == null || key == null) {
              break;
            }
            //codeArea.append(urlConnection.getHeaderFieldKey(count) + ": " + header + "\n");
            count++;
          }
          InputStream in = new BufferedInputStream(urlConnection.getInputStream());
          Reader r = new InputStreamReader(in);
          int c;
          while ((c = r.read()) != -1) {
            source = String.valueOf((char) c);
                System.out.print(source]);
                codeArea.append(source);
                //outStream.print(source);
            
            //System.out.print(source);
          }//outStream.close();
          //codeArea.setCaretPosition(1);
        } catch (Exception ee) {}
      }
    });
  }
}

Ok, I've done some more testing on it today. I don't think the problem has anything to do with what it is doing, but simply not getting the click of the button. I added an output line after I clicked the button to say "it worked." It still works fine in netbeans, but doesn't even show that text in the browser.

I copied the code and started over. Now it when I click the button, it displays that it worked, but doesn't show the source of the file in the browser, but works perfectly in netbeans.

Do I have to get this applet signed?

Applets can only make network connections back to the server on which they are hosted unless they are signed, so yes you need to make it a signed applet.

Thanks for your reply. I've been working the past few hours trying to get it signed by using the jarsigner, but I'm I can't even get a prompt to come up when I run the applet that asks me if I trust it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.