views:

1638

answers:

3

I want to make an Applet write to a text file. I think have overcome the security problem with my own certificate, but the text file isn't receiving the output.

My Applet can be seen at tomrenn.com/fallball, and should ask if you trust the certificate.

I'm not sure an Applet can write to a text file within the web hosted contents, so I'm asking if this is possible; and if not, at least be able to do it on the local machine. I had been using a FileOutputStream, but my teacher recommended this very basic output method.

public void writeToFile()
{
  try {
     java.io.File file = new java.io.File("highscores.txt");
     java.io.PrintWriter output = new java.io.PrintWriter(file);
     output.print(name + " " +score);
     output.close();
  } catch (IOException e) {
     System.err.println ("Unable to write to file");
     System.exit(-1);
  }
}

This is not what I would be implementing, just trying to see if I get any output. Thanks for looking over my question!

+3  A: 

From an Applet, you cannot directly write to the server's file system. You can issue a request to the server that causes the server to write to its own file system, but an Applet does not have a way to write to a file system on a remote machine. (Of course, unless it's mounted NFS or otherwise.) To issue such a request, you could use Apache HttpClient to issue HTTP requests, for example. This may be more heavyweight than you are looking for. You can also have the client issue a POST to the server to say, "This is my high score," and let the server manage high scores.

A signed Applet has every right to write to the local file system of the person running the Applet. If you are writing to the "current directory" (rather than an absolute full path), then make sure you know what directory the Applet is running in. Otherwise you may indeed create a file, but not be able to find it!

Eddie
+1  A: 

Applets run on the client so cannot access the servers disk. The code you posted will write to the clients local disk. I'd suggest changing it though to specify the directory you want to place the file. The users home directory would seem a good place for it

java.io.File file = new java.io.File(System.getProperty("user.home"), "highscores.txt");
objects
+2  A: 

If you want an applet to store data on the local machine, from 6u10 the javax.jnlp.PersistenceService is available. Creating a secure "signed applet" is difficult, and I wouldn't recommend it to anyone.

Tom Hawtin - tackline
@finnw Oh signing an applet, you can just follow the tutorial. Making sure your "signed applet" is secure, is a little more tricky, although feasible since 6u19.
Tom Hawtin - tackline
@Tom Hawtin, please explain what you mean
finnw
@finnw Security is like difficult. It does not happen automatically.
Tom Hawtin - tackline
Please at least provide a link to an article explaining the pitfalls, and please explain what has changed in 6u19 that makes it "feasible". The only relevant change I am aware of is to the root certificates. Just saying "it's difficult" is not helpful at all.
finnw
@finnw http://download-llnw.oracle.com/javase/6/docs/technotes/guides/jweb/mixed_code.html
Tom Hawtin - tackline
If I understand correctly this would only affect an applet that uses third-party extensions from another site, and this could be worked around in versions 6u10-6u18 by hosting local copies of the extension, re-signed with the applet vendor's certificate. Does that make sense? For versions prior to 6u10 I have always combined all code into a single jar.
finnw
There are no signed applets, only signed jars (and JNLP files). Prior to 6u19 there is nothing to stop malicious code using "signed applets" as if they were extension.
Tom Hawtin - tackline