We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Working with file i/o in applets
Page Index Toggle Pages: 1
Working with file i/o in applets (Read 1783 times)
Working with file i/o in applets
Jul 29th, 2009, 10:49am
 
In my applet, I would like users to be able to save a "profile" of their settings so when they come back they can log in and their settings will already be available to them. All I need to do this is a file called users.txt which stores each user name and its corresponding password. I thought I could save that internally inside the app and recall it later, but apparently that's not the case. All my jars are signed, but I'm getting the following line in the console:

java.io.FileNotFoundException: C:\Program Files (x86)\Mozilla Firefox\users.txt (Access is denied)

I'm not sure why it's looking in the Firefox folder, and maybe someone can help me with that. Is it not possible to write a .txt file inside the app itself? Should I be saving the file using the savetoweb hack instead?
Re: Working with file i/o in applets
Reply #1 - Jul 29th, 2009, 1:36pm
 
I just gave the savetoweb stuff a shot, and I couldn't get that to work either. Anyone have any tips on how I can fix this?
Re: Working with file i/o in applets
Reply #2 - Jul 29th, 2009, 1:54pm
 
Saving a file from an applet is not obvious, since it depends on system. Perhaps you can spawn a JFileChooser to let the user choose the saving location.

A possible alternative way is to use cookies.

Apparently, you can use also the Preferences API with privileged mode. I see in this thread somebody else suggesting to use cookies.

And if you have an issue with saveToWeb (which version), you should show how you tried to do it.
Re: Working with file i/o in applets
Reply #3 - Jul 29th, 2009, 7:44pm
 
Thanks for the links, PhiLho. My first reaction to this problem was originally "cookie!", but I was having trouble finding some good documentation. The link you provided was great, and I've got something up and running as seen below. Here's my next question, however. I've never used JSObjects before. If I'm going to be saving preferences for multiple users, should I create multiple JSObjects, or should I just keep adding cookies to my original JSObject via the setMember() function?

Code:

import netscape.javascript.*;

void setup(){
 size(100,100);
 JSObject myBrowser = JSObject.getWindow(this);
 JSObject myDocument = (JSObject)myBrowser.getMember("document");
 myDocument.setMember("cookie", "username");
 getCookie();
}

void draw(){
 
}

String getCookie(){
 try{
   JSObject myBrowser = (JSObject)JSObject.getWindow(this);
   JSObject myDocument = (JSObject)myBrowser.getMember("document");
   String myCookie = (String)myDocument.getMember("cookie");
   if (myCookie.length() > 0){
     println("my cookie = "+myCookie);
     return myCookie;
   }
 }
 catch (Exception e){
   e.printStackTrace();
 }
 return "?";
}

String getCookie(String name){
 String myCookie = getCookie();
 String search = name + "=";
 if(myCookie.length() > 0){
   int offset = myCookie.indexOf(search);
   if(offset != 1){
     offset += search.length();
     int end = myCookie.indexOf(";", offset);
     if(end == -1) end = myCookie.length();
     println("my cookie = "+myCookie.substring(offset,end));
     return myCookie.substring(offset,end);
   }
   else {
     println("Did not find cookie: "+name);
   }
 }
 return "";
}
Re: Working with file i/o in applets
Reply #4 - Jul 30th, 2009, 1:19am
 
myDocument is a reference to the current Web page holding the applet, so you don't need to create additional JSObjects, you just re-use this one and create different cookies. Supposing, of course, there are several different people using the same applet on the same browser.
Re: Working with file i/o in applets
Reply #5 - Jul 30th, 2009, 7:10am
 
OK...next question, then.  Wink This is a public app that anyone using any browser can access. So! Does this mean that there will be multiple JSObjects for each browser that's used, but that everyone using the same browser will have their data located in the same JSObject?

So in other words, as I'm thinking through how to access everyone's data on later visits, I would basically test for which browser the user is on, then load up the corresponding JSObject? Then, since I have a login screen already up and running, I'm thinking I would save each username as the cookie name, and their password as the cookie data. So once I get their username and password strings, I would use the getCookie(String _name) method below to call up the cookie with their name on it, then test to see if their password matches the cookie data. Does that sound right?
Re: Working with file i/o in applets
Reply #6 - Jul 30th, 2009, 10:39am
 
You don't have to test which browser is used: cookies work the same for all browsers.
Your applet storing cookies stores it only for your site, on the given browser: you cannot see the cookies of the other sites, and if the users go to your site with another browser, the cookies have to be made again.
So you need a user name and password only if you expect to have several users using the same browser on the same computer to use your applet.

Note: if you want a minimal security (a user not looking at the password of another user), at least store a hash of the password, and compare the hashes, not the passwords.
Re: Working with file i/o in applets
Reply #7 - Jul 30th, 2009, 11:12am
 
You're right. I'm an idiot. I didn't think through this whole thing before I replied. I forgot cookies are individual files stored on the local machine of THE USER. I was assuming that all of this would be stored in a server file, instead of as part of individual applet instances on each user's local machine. So really (as you've already stated) there should be no need for a user login...each user's preferences should just be loaded automatically.

I think I should be good to go now. Thanks for your help (as always  Roll Eyes ) PhiLho...
Page Index Toggle Pages: 1