This magercise takes you through the steps necessary to allow an applet to write to a local file. Using policytool
, you open up
a selected part of the Java security model to permit this capability. The JRE's access controller will not permit access to other files.
Compile the
PLTester.java program. It contains a JTextField
that contains a file to save to, a
JTextArea
that contains the text to save, and a JButton
to save the text area to the file specified by the text field, when
the button is selected.
Save the
PLTester.html loader and try the program without using policytool
. Select Save to try
to save the contents to any filename entered. This will generate a security violation.
Start up policytool
.
Open the real policy file.
Select Add Policy Entry and enter "file:." in the CodeBase field. This means you are updating the security policy for classes
loaded from the current directory.
Next select Add Permission to bring up the Add New Permissions screen.
For a permission, select FilePermission in the first option.
For a target, enter the filename you wish to make writable.
For the actions option, either select write or enter write in the field to its right.
Then select OK to close this window.
On the Policy Entry window, select Done.
On the opening screen, select File|Save to update the policy file.
At this point, rerun the test applet and see if the file is writable. Try different values for the filename to ensure ONLY
that file is writable and nothing else.
After testing the applet, be sure to remove the entry from the policy file with policytool
. Just select the appropriate code base
from the policy file then select Remove Policy Entry to get rid of it and confirm the removal. Next select File|Save to save the
updated policies.