Dooj
asked on
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,thi s);
}
catch (IOException e) {
System.out.println("Except ion 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("Outs ide While");
String str = new String(url);
if(str.equals(newurl))
{
System.out.println("Hooora y!!!");
URL redirecturl = new URL(str);
getAppletContext().showDoc ument(redi recturl);
this.stop();
}
else
{
System.out.println("Try Again!");
URL redirecturl1 = new URL(redirectUrl);
getAppletContext().showDoc ument(redi recturl1);
this.stop();
}
}catch (Exception e) {System.out.println("Excep tion 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
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,thi
}
catch (IOException e) {
System.out.println("Except
}
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
try
{
System.out.println("Inside
InputStream in = readUrl.openStream();
while ((a = in.read()) != -1)
{
url.append((char)a);
}
//System.out.println("Outs
String str = new String(url);
if(str.equals(newurl))
{
System.out.println("Hooora
URL redirecturl = new URL(str);
getAppletContext().showDoc
this.stop();
}
else
{
System.out.println("Try Again!");
URL redirecturl1 = new URL(redirectUrl);
getAppletContext().showDoc
this.stop();
}
}catch (Exception e) {System.out.println("Excep
// e.printStackTrace();
}
}
}
Thread thread;
String line;
// int a;
StringBuffer url;
String newurl;
URL readurl;
// InputStream in;
// ShowUrl show;
}
Thanks for your help.
DD
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 :-)
Anyway, thanks heaps Steve.
Cheers, DD :-)