Security Exception - Whats wrong with my code?

Hello,

Could some one tell me what is wrong with the following code? It is driving me mad. It compiles ok but gives an error when I start the applet.



import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class ThreadTest extends Applet
{
  public void init()
  {
   try
   {
    url = new StringBuffer();
    readurl = new URL("http://xxx.cxxx.net/me/test.txt");
    newurl = "http://www.yyy.com";
    System.out.println("The URL is " + readurl);
    ShowURL show = new ShowURL(newurl,readurl,this);
   }
   catch (IOException e) {    
     System.out.println("Exception error in the prog " + e);}
 }

 class ShowURL extends Thread
 {  
  String redirectUrl;
  URL readUrl;
  Applet applet;
  //InputStream in;
  int a;
  public ShowURL(String redirectUrl, URL readUrl, Applet applet)
  {
   this.redirectUrl = redirectUrl;
   this.readUrl = readUrl;
   this.applet = applet;
   this.start();
  }
  public void run()
  {
   System.out.println("Inside Run");
   try
   {
    System.out.println("Inside Try");
    InputStream in = readUrl.openStream();
    while ((a = in.read()) != -1)
    {
     url.append((char)a);
    }
    //System.out.println("Outside While");
    String str = new String(url);
    if(str.equals(newurl))
    {
     System.out.println("Hoooray!!!");
     URL redirecturl = new URL(str);
     getAppletContext().showDocument(redirecturl);
      this.stop();
    }
    else
    {      
     System.out.println("Try Again!");
     URL redirecturl1 = new URL(redirectUrl);
     getAppletContext().showDocument(redirecturl1);
     this.stop();
    }            
   }catch (Exception e) {System.out.println("Exception error in the prog " + e);
   //      e.printStackTrace();
   }      
                    

 }
   
}    
 

Thread thread;
String line;
// int a;
StringBuffer url;
String newurl;
URL readurl;
// InputStream in;
// ShowUrl show;
 }


Thanks for your help.

DD
DoojAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
I would expect this applet to give a security error. Unless an applet is signed it can only access the machine it was loaded from.
0
s_franklinCommented:
You're running into restrictions of the applet. If you ran it locally as an application you would have no problems. It compiles fine because it is valid. However the security container around applets as enforced by the browsers doesn't let them do this.

There is some good information that you will want to read on the topic:

Java Net Programming FAQ 2.7 - "Why is a security exception thrown when using java.net.URL or java.net.URLConnection from an applet?"

http://www.davidreilly.com/java/java_network_programming/#2.7

If you want to do this, you should look into signing your applets. Signed applets can accomplish what you want legitimately. Some information on this topic can be found at:

http://www.suitable.com/Doc_CodeSigning.shtml

Some browsers let you override this restriction such that the JVM doesn't get so picky. I wouldn't rely on this as part of my design however - users can turn it on and off at will, and it isn't available for all browsers.

Steve

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DoojAuthor Commented:
Yeah, that makes sense. So, if I load this in the same server as the text file (which has to be read) I should not get this error, right?

Anyway, thanks heaps Steve.

Cheers, DD :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.