aspose file tools*
The moose likes Applets and the fly likes Writing a properties file with an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Writing a properties file with an applet" Watch "Writing a properties file with an applet" New topic
Author

Writing a properties file with an applet

Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
I am trying to use Properties through an Applet. I have signed the Applet and I am able to read a file from the getCodeBase() URL, but I am not able to write this file. I know that I can update the java.policy file but I am looking for a way around because everyone that viewed the applet would nee to update the policy file. Is there anyway to write without changing the policy, or can I write something for the end user to run that would automatically update the policy file on that computer so they could read write the properties?

Thanks
-Derrick
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

This "file" is on the server, is it not? It must be if you're getting it from the applet's code base.

So to write the file, the applet would have to upload the data to some server component which took care of actually writing to that file. You can't just write a file to a different computer (i.e. your server), the security issues should be obvious. You need something running on the server which accepts the file transfer. If you're in the Java ecosystem then a servlet might answer. Google keywords: file upload servlet.

You might also consider what happens when you have two, or two hundred, users of the applet all updating that file. What's the rule? Last update wins? What happens with two near-simultaneous updates?
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
Thanks for responding, actually I need something local, I have tried cookies, but, well that sucks because the site is limited to 20 cookies and there are several object states that I need to save, I really need local file access with out each user needing to understand how to update the java.policy. I thought signing the applet would be the answer, but they would still have to access the java.policy file and this isn't a real option. Any suggestions?

Thanks,
-Derrick
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Derrick Albers wrote:I thought signing the applet would be the answer, but they would still have to access the java.policy file and this isn't a real option.

Can you elaborate on why you think signing isn't the answer? All users can fiddle with their local policy files if they so choose, but very few do so, especially if it isn't required.
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
I can't ask the use to change anything on their end. I signed the applet and I am still denied the ability to write a props file on the local system, i.e. c://myprop.props. Everything I have found tells me that I have to sign it, and that the user will have to add that applet to the java.policy file. The user base is a click and go base so they can't be asked to alter their systems. Accepting the signed applet was a huge decision, editing a file would be out of the question.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
No, that's not how it works; signing an applet is sufficient to grant it all permissions. (And it is really the only way - asking an average user to fiddle with policy files is a non-starter.)

If signing by itself won't do, have a read of http://www.coderanch.com/how-to/java/HowCanAnAppletReadFilesOnTheLocalFileSystem and apply the privileged code tweak it describes; that should do the trick.
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
That's great! Thank you for the help I am going to try this right now.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

As an aside: have you considered using the java.util.preferences package to store these properties? This (at least for Windows) puts data into the registry. You'd still have to sign the applet to do that, but you wouldn't have to deal with the possibility of not being able to create a file in a particular place because of directory permissions and so on.
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
Problem there is that it is run on MAC, Linux, and Windows. jre 1.4, 5, and 6
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

Then on the non-Windows boxes the preferences are stored somewhere else. It works there too, I just don't know the details.

That would also be simpler for you because it abstracts all the details of exactly where to put the preferences away and you don't have to deal with that.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Paul makes an excellent point. The Preferences API was introduced in Java 1.4, so it works on all platforms you mention.
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
Thank you guys for the help I am going to use this to do it, I will let you know how it turns out.
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
I ran into a problem with using the privlaged code:

How can I pass the string into this and get the return? I need to use prefs.put(key,value) and return the string value from prefs.get(key)
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
A class like the following would help; whether a get or a put is performed depends on whether or not a null value is passed in for the value. You'll need to make the "prefs" object in your applet code final, or the compiler will complain.
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
That worked like a charm, thanks again to everyone for all of your help!

Thanks
-Derrick
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
Well after I get that working they wanted it changed....of course So now I have to write a file that is to be named what I given from the code and place it in the a directory on the client side computer, is this possible from an applet?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

Sure. It would have to be a signed applet, of course, and there are always potential problems such as the applet not having the right permissions to write into that directory and so on, but yeah, signed applets can write to the client's file system as much as any other application can.
Derrick Albers
Greenhorn

Joined: Nov 01, 2010
Posts: 20
Yea I got it, I had to get the URI.toURL().openStream() to read it, but I figured it out Thanks for the response though I appreciate you guy always helping out!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing a properties file with an applet
 
Similar Threads
(Anyone) using propertyfile ?
Writing to a file
Applet Security
Writing to a text file contained in the jar?
Problem loading policy file programmatically