I have tried the certificate solution:
javac HelloWorld.java
jar cvf HelloWorld.jar HelloWorld.class
jarsigner -keystore myKeystore HelloWorld.jar myself
and placed the jar and the html file in my web server.
See codes:
index.html:
<html>
<head>
</head>
<body>
<applet archive="HelloWorld.jar" code="HelloWorld.class" width=150
height=50></applet>
<br>
<br>
In the applet field above it should appear the text :
<br>
<br>
<html><head>
<br>
<br>
<br>
instead, you can see:
<br>
<br>
Empty Text
<br>
<br>
</body>
</html>
HelloWorld.java:
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.net.*;
import java.io.*;
public class HelloWorld extends Applet {
//This causes the Browser to Redirect, and is not what we want...
//public void init() {
//
//AppletContext appletContext;
//URL my_url;
// try {
// my_url = new URL("
http://www.yahoo.com");
// appletContext = getAppletContext();
// appletContext.showDocument(my_url);
// } catch (Exception e) {
// System.out.println("Exception!");
// }
//}
URL MyURL;
BufferedReader InFile;
String inputLine;
public void init() {
inputLine = "Empty Text";
try {
MyURL = new URL("
http://www.yahoo.com");
InFile = new BufferedReader(new InputStreamReader(MyURL.openStream()));
inputLine = InFile.readLine();
} catch (Exception MyException) {
System.out.println(MyException);
}
}
public void paint(Graphics g) {
g.drawString(inputLine, 50,25);
}
}
When I browse that index.html file, I get the "Empty Text" string, and
not the first line of the
www.yahoo.com server...
Am I doing something wrong?...
Use a test certificate -- go to java.sun.com and read how to create a test
certificate and use it to digitially sign. The other alternative, for you to
test locally, is to run it as an application. To do this, put a main() in
the applet, which instantiates the applet, as follows:
public class MyApplet extends Applet{
public boolean isAnApplet;
public class MyApplet(){
isAnApplet=true;
}
public static void main(String []args) {
MyApplet applet = new MyApplet ();
JFrame mainFrame = new JFrame(applet.getClass().getName());
applet.isAnApplet=false; //VITAL! this is a MyApplet class member
variable which you can use throughtout your code to tell you if you are
running in applet or application mode.
applet.init();
applet.start();
mainFrame.getContentPane().add(applet);
mainFrame.setSize(applet.windowsize); //width, height
mainFrame.setVisible(true);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//now you can run it as an applet or an applicatrion. If you run as an
applet, to circumvent the security restrictions, digitally sign it - even
with a test certificate. -Ike
Francisco Camarero said:
I am writing a very small Applet that reads some files via
http from different servers and reports some result.
I am using the usual code:
..
MyURL = new URL("
http://www.somewhere.com/somepath/somefile");
InFile = new BufferedReader(new InputStreamReader(MyURL.openStream()));
inputLine = InFile.readLine();
..
But, when executing it with appletviewer I get exceptions:
java.security.AccessControlException: access denied (java.net.SocketPermission
www.somewhere.com resolve)
And from the Browser it doesn't work either.
I have read somewhere that this is normal... that because of
security, the applet can't read via http from other servers...
I have also read something about a workaround with servlets installed
on the servers I want to read from, but I don't control these servers,
so I can't install nothing there...
Any other solution or definitive negative ?
Thanks!
Fran.
--
Francisco Camarero GSM: +41 764 5432 78
ETZ J86, Gloriastrasse 35 telephone: +41 1 632 76 50
CH-8092 Zurich telefax: +41 1 632 11 94
Switzerland e-mail: (e-mail address removed)