boatful
asked on
CodeBase Enabled--still applet hangs!
Hi,
awilkins (EE Expert) provided the following Applet for writing/reading to disk with Java (which I compiled as "cookin.class". For development purposes I am trying to run it locally without signing it. I followed directions explicitly from Netscape's Alec Plumb for activating codebase principals in NN 4.06, and wrote a simple HTML page to call the applet functions (see "test.htm" below). When I place the test.htm and cookin.class files in the same directory and load the former into NN 4...
a. Status bar reads "running applet..."
b. the phrase "Setting up..." displays as expected
c. then......nothing. Status bar reads "document done".
************************** ********** ********** *
here's the applet: cookin.java (cookin.class)...
import java.applet.Applet;
import java.io.*;
public class cookin extends Applet
{
String baseDir; // base Directory for all your cookies to be placed in.
public void init()
{
baseDir = "C:\\temp"; // change as desired
}
public void writeCookie(String fileName, String value)
{
try {
FileWriter fileWrite = new FileWriter(baseDir + fileName);
fileWrite.write(value);
} catch (IOException e) {
System.out.println("Error writing a cookie");
System.out.println(e.toStr ing());
}
}
public String readCookie(String fileName)
{
try {
BufferedReader fileRead = new BufferedReader(new FileReader(baseDir + fileName));
StringBuffer strb = new StringBuffer(""); // creates an initially blank StringBuffer
while (fileRead.ready()) // while more remains to be read
strb.append(fileRead.readL ine());
return new String(strb); // converts strb to a String
} catch (IOException e) {
System.out.println("Error reading a cookie");
System.out.println(e.toStr ing());
return "Error reading a cookie";
}
}
public void deleteCookie(String fileName)
{
File f = new File(baseDir + fileName);
f.delete();
}
}
************************** ********** ********** *
here's test.htm...
<HTML>
<HEAD>
<TITLE>Testing 'cookin' applet</TITLE>
</HEAD>
<BODY>
<applet code="cookin.class" name="cookin" width=300 height=2>
</applet>
<script language = "JavaScript">
<!--
document.write("Setting up...");
document.cookin.writeCooki e("testfil e.txt", "It works!");
document.write("File Written...");
var x = document.cookin.readCookie ("testfile .txt");
document.write("File Read...");
alert("Recalled value ="+x);
// -->
</script>
</BODY>
</HTML>
CAN ANYONE HELP ME GET THIS FILE READ/WRITE APPLET WORKING??
awilkins (EE Expert) provided the following Applet for writing/reading to disk with Java (which I compiled as "cookin.class". For development purposes I am trying to run it locally without signing it. I followed directions explicitly from Netscape's Alec Plumb for activating codebase principals in NN 4.06, and wrote a simple HTML page to call the applet functions (see "test.htm" below). When I place the test.htm and cookin.class files in the same directory and load the former into NN 4...
a. Status bar reads "running applet..."
b. the phrase "Setting up..." displays as expected
c. then......nothing. Status bar reads "document done".
**************************
here's the applet: cookin.java (cookin.class)...
import java.applet.Applet;
import java.io.*;
public class cookin extends Applet
{
String baseDir; // base Directory for all your cookies to be placed in.
public void init()
{
baseDir = "C:\\temp"; // change as desired
}
public void writeCookie(String fileName, String value)
{
try {
FileWriter fileWrite = new FileWriter(baseDir + fileName);
fileWrite.write(value);
} catch (IOException e) {
System.out.println("Error writing a cookie");
System.out.println(e.toStr
}
}
public String readCookie(String fileName)
{
try {
BufferedReader fileRead = new BufferedReader(new FileReader(baseDir + fileName));
StringBuffer strb = new StringBuffer(""); // creates an initially blank StringBuffer
while (fileRead.ready()) // while more remains to be read
strb.append(fileRead.readL
return new String(strb); // converts strb to a String
} catch (IOException e) {
System.out.println("Error reading a cookie");
System.out.println(e.toStr
return "Error reading a cookie";
}
}
public void deleteCookie(String fileName)
{
File f = new File(baseDir + fileName);
f.delete();
}
}
**************************
here's test.htm...
<HTML>
<HEAD>
<TITLE>Testing 'cookin' applet</TITLE>
</HEAD>
<BODY>
<applet code="cookin.class" name="cookin" width=300 height=2>
</applet>
<script language = "JavaScript">
<!--
document.write("Setting up...");
document.cookin.writeCooki
document.write("File Written...");
var x = document.cookin.readCookie
document.write("File Read...");
alert("Recalled value ="+x);
// -->
</script>
</BODY>
</HTML>
CAN ANYONE HELP ME GET THIS FILE READ/WRITE APPLET WORKING??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1. netscape.security.Privileg eManager is necessary for signed applets in Netscape 4.0x
2. FileWriter will create a file if it doesn't exist, but not create the directory which the file needs.
We can use File.mkDir() to create the directory
3. I can't get the point of your question
It seems your Java didn't run...
use more System.out.println(...) in your Java to ensure the methods were invoked.
2. FileWriter will create a file if it doesn't exist, but not create the directory which the file needs.
We can use File.mkDir() to create the directory
3. I can't get the point of your question
It seems your Java didn't run...
use more System.out.println(...) in your Java to ensure the methods were invoked.
ASKER
Hi again,
You're a step ahead of me Threshold. You see, I can't seem to consolidate your suggestions into the original applet correctly (thus it won't even compile). If you'd be willing to piece the two together using the correct order and syntax, that would be great. I'd be glad to double the points if you can help me get this applet running. I need it to sidestep the limitations of cookies in JavaScript, so that I can save more that 20 'cookies' per domain, and save datastrings longer that 4 kb. If you're interested in the application I'm building, you may check it out at http://users.aol.com/beaconess/HEP/. If I sound ignorant about Java that's quite accurate. I'm pretty fluent in JavaScript, and I intend only to learn as much Java as is necessary to save long datastrings (and lots of them) in my program.
Question #3 is: do I send as a filename "mystring" or "mystring.txt"?
Thanks...
Tony
You're a step ahead of me Threshold. You see, I can't seem to consolidate your suggestions into the original applet correctly (thus it won't even compile). If you'd be willing to piece the two together using the correct order and syntax, that would be great. I'd be glad to double the points if you can help me get this applet running. I need it to sidestep the limitations of cookies in JavaScript, so that I can save more that 20 'cookies' per domain, and save datastrings longer that 4 kb. If you're interested in the application I'm building, you may check it out at http://users.aol.com/beaconess/HEP/. If I sound ignorant about Java that's quite accurate. I'm pretty fluent in JavaScript, and I intend only to learn as much Java as is necessary to save long datastrings (and lots of them) in my program.
Question #3 is: do I send as a filename "mystring" or "mystring.txt"?
Thanks...
Tony
ASKER
Looking good Threshold!
I pieced together the applet so that netscape.security.* imports and when I run it I get the GRANT PRIVILEGES dialog. So far so good!
The Java Console contains 'Success!', so I know the init() object is working (see below). The writeCookie() object seems to hang, however (no file is written and no error messages show up in the Java console). I'll experiment with adding System.out.println(...) statements there.
Stay tuned...
APPLET AS OF 9/7 (HAPPY LABOR DAY)
import java.applet.Applet;
import java.io.*;
import netscape.security.*;
public class cookin3 extends Applet
{
String baseDir = "C:\\temp"; // base Directory for all your cookies to be placed in.
public void init()
{
try {
PrivilegeManager.enablePri vilege("Un iversalFil eAccess"); // high risk!!
System.out.println("\tSucc ess!");
} catch (netscape.security.Forbidd enTargetEx ception e) {
System.out.println("\tFail ed! Permission to read system properties denied by user.");
} catch (Exception e) {
System.out.println("\tFail ed! Unknown exception while enabling privilege.");
e.printStackTrace();
}
}
public void writeCookie(String fileName, String value)
{
try {
FileWriter fileWrite = new FileWriter(baseDir + fileName);
fileWrite.write(value);
} catch (IOException e) {
System.out.println("Error writing a cookie");
System.out.println(e.toStr ing());
}
}
public String readCookie(String fileName)
{
try {
BufferedReader fileRead = new BufferedReader(new FileReader(baseDir + fileName));
StringBuffer strb = new StringBuffer(""); // creates an initially blank StringBuffer
while (fileRead.ready()) // while more remains to be read
strb.append(fileRead.readL ine());
return new String(strb); // converts strb to a String
} catch (IOException e) {
System.out.println("Error reading a cookie");
System.out.println(e.toStr ing());
return "Error reading a cookie";
}
}
public void deleteCookie(String fileName)
{
File f = new File(baseDir + fileName);
f.delete();
}
}
I pieced together the applet so that netscape.security.* imports and when I run it I get the GRANT PRIVILEGES dialog. So far so good!
The Java Console contains 'Success!', so I know the init() object is working (see below). The writeCookie() object seems to hang, however (no file is written and no error messages show up in the Java console). I'll experiment with adding System.out.println(...) statements there.
Stay tuned...
APPLET AS OF 9/7 (HAPPY LABOR DAY)
import java.applet.Applet;
import java.io.*;
import netscape.security.*;
public class cookin3 extends Applet
{
String baseDir = "C:\\temp"; // base Directory for all your cookies to be placed in.
public void init()
{
try {
PrivilegeManager.enablePri
System.out.println("\tSucc
} catch (netscape.security.Forbidd
System.out.println("\tFail
} catch (Exception e) {
System.out.println("\tFail
e.printStackTrace();
}
}
public void writeCookie(String fileName, String value)
{
try {
FileWriter fileWrite = new FileWriter(baseDir + fileName);
fileWrite.write(value);
} catch (IOException e) {
System.out.println("Error writing a cookie");
System.out.println(e.toStr
}
}
public String readCookie(String fileName)
{
try {
BufferedReader fileRead = new BufferedReader(new FileReader(baseDir + fileName));
StringBuffer strb = new StringBuffer(""); // creates an initially blank StringBuffer
while (fileRead.ready()) // while more remains to be read
strb.append(fileRead.readL
return new String(strb); // converts strb to a String
} catch (IOException e) {
System.out.println("Error reading a cookie");
System.out.println(e.toStr
return "Error reading a cookie";
}
}
public void deleteCookie(String fileName)
{
File f = new File(baseDir + fileName);
f.delete();
}
}
ASKER
the latest...
I modified the writeCookie object as follows...
public void writeCookie(String fileName, String value)
{
try {
System.out.println("\ttryi ng to instantiate fileWrite...");
FileWriter fileWrite = new FileWriter(baseDir + fileName);
System.out.println("\tfile Write instantiated!");
fileWrite.write(value);
System.out.println("\t"+va lue+" written to "+baseDir+fileName);
} catch (IOException e) {
System.out.println("Error writing a cookie");
System.out.println(e.toStr ing());
}
}
but all I ever get in the java console is "trying to instantiate fileWrite...". I NEED YOUR HELP !!!
Tony
I modified the writeCookie object as follows...
public void writeCookie(String fileName, String value)
{
try {
System.out.println("\ttryi
FileWriter fileWrite = new FileWriter(baseDir + fileName);
System.out.println("\tfile
fileWrite.write(value);
System.out.println("\t"+va
} catch (IOException e) {
System.out.println("Error writing a cookie");
System.out.println(e.toStr
}
}
but all I ever get in the java console is "trying to instantiate fileWrite...". I NEED YOUR HELP !!!
Tony
ASKER
OK, Threshold...
I'm closer still. I got a lesson in inheritance from MSMOLYAK and now the writeCookie method actually creates a blank file in the specified directory, however the method doesn't write the string 'value' to the new file. Unfortunately, no exception is thrown either. Can you (or anyone else) diagnose...???
I'm closer still. I got a lesson in inheritance from MSMOLYAK and now the writeCookie method actually creates a blank file in the specified directory, however the method doesn't write the string 'value' to the new file. Unfortunately, no exception is thrown either. Can you (or anyone else) diagnose...???
If you got a blank file, the most possible reason is that you didn't close it.
All File I/O will be cached in memory by OS, we have to use the methods like 'close' or 'flush' to make sure the OS write the data to disk.
All File I/O will be cached in memory by OS, we have to use the methods like 'close' or 'flush' to make sure the OS write the data to disk.
FileWriter fileWrite = new FileWriter(baseDir + fileName);
fileWrite.write(value);
fileWrite.close(); // ------------------------> it's needed!
fileWrite.write(value);
fileWrite.close(); // ------------------------> it's needed!
ASKER
Thanks kindly for all your support and encouragement. I now have a single applet capable of file and folder read/write/list/delete (see below). I want you to have the points, but I have a couple of follow-up questions even so.
First, is it possible to request limited access to I/O functions, e.g. I/O exclusively to a floppy drive??
What do I need to modify for the applet to work in MSIE 4.0??
JavaScript string methods don't work on values returned by the applet unless I use a phrase like:
newstring = unescape(escape(document.c ookin.ioma nager(3,"" ,"testfile ",""))). Is there a more efficient way??
Here's the applet:
import java.applet.Applet;
import java.io.*;
import netscape.security.*;
public class cookie extends Applet
{
String baseDir = "C:\\TheraLnk\\program"; // base Directory for all your cookies to be placed in.
String retval = "dummy";
public String iomanager(int task, String folderName, String fileName, String val)
{
if (task <=5)
{
try {
PrivilegeManager.enablePri vilege("Un iversalFil eAccess"); // high risk!!
System.out.println("\tSucc ess!");
} catch (netscape.security.Forbidd enTargetEx ception e) {
System.out.println("\tFail ed! Permission to read system properties denied by user.");
} catch (Exception e) {
System.out.println("\tFail ed! Unknown exception while enabling privilege.");
e.printStackTrace();
}
retval = new String("init");
}
if (task == 1)
{
try {
File td = new File(baseDir+folderName);
if (td.mkdir())
System.out.println("\tpowe rfolder instantiated!"); //MESSAGE 2
retval = new String("dir");
}
catch (SecurityException e) {
System.out.println("Error making a folder"); //MESSAGE 4
System.out.println(e.toStr ing()); //MESSAGE 5
retval = new String("dir err");
}
}
if (task == 2)
{
try {
FileWriter powerCookie = new FileWriter(baseDir + folderName + fileName);
System.out.println("\tpowe rCookie instantiated!"); //MESSAGE 2
powerCookie.write(val);
powerCookie.close();
System.out.println("\t"+va l+" written to "+baseDir + folderName + fileName); //MESSAGE 3;
retval = new String("write");
} catch (IOException e) {
System.out.println("Error writing a cookie"); //MESSAGE 4
System.out.println(e.toStr ing()); //MESSAGE 5
retval = new String("write err");
}
}
if (task == 3)
{
try {
BufferedReader fileRead = new BufferedReader(new FileReader(baseDir + folderName + fileName));
//the default buffer length crumps after about 2610th character in a string.
StringBuffer strb = new StringBuffer(""); // creates an initially blank StringBuffer
while (fileRead.ready()) // while more remains to be read
strb.append(fileRead.readL ine());
retval = new String(strb); // converts strb to a String
} catch (IOException e) {
System.out.println("Error reading a cookie");
System.out.println(e.toStr ing());
retval = new String("read err");
}
}
if (task == 4)
{
System.out.println("\tDele ting... "+baseDir + folderName + fileName);
File f = new File(baseDir + folderName + fileName);
f.delete();
retval = new String("del");
}
if (task == 5)
{
try {
System.out.println("Starti ng filelist routine");
File d = new File(baseDir + folderName);
String files[] = null;
files = d.list();
System.out.println("Succes s on File.list. Found " + files.length + " files in " + d.toString());
retval = "";
for (int i = 0; i < files.length; i++) {
if (i !=0) {
retval += "*~&!^H@%#$";
}
retval += files[i].toString();
}
System.out.println(retval) ;
}
catch (SecurityException e) {
System.out.println("Caught security exception on File.list");
retval = "list err";
}
}
return retval;
}
}
First, is it possible to request limited access to I/O functions, e.g. I/O exclusively to a floppy drive??
What do I need to modify for the applet to work in MSIE 4.0??
JavaScript string methods don't work on values returned by the applet unless I use a phrase like:
newstring = unescape(escape(document.c
Here's the applet:
import java.applet.Applet;
import java.io.*;
import netscape.security.*;
public class cookie extends Applet
{
String baseDir = "C:\\TheraLnk\\program"; // base Directory for all your cookies to be placed in.
String retval = "dummy";
public String iomanager(int task, String folderName, String fileName, String val)
{
if (task <=5)
{
try {
PrivilegeManager.enablePri
System.out.println("\tSucc
} catch (netscape.security.Forbidd
System.out.println("\tFail
} catch (Exception e) {
System.out.println("\tFail
e.printStackTrace();
}
retval = new String("init");
}
if (task == 1)
{
try {
File td = new File(baseDir+folderName);
if (td.mkdir())
System.out.println("\tpowe
retval = new String("dir");
}
catch (SecurityException e) {
System.out.println("Error making a folder"); //MESSAGE 4
System.out.println(e.toStr
retval = new String("dir err");
}
}
if (task == 2)
{
try {
FileWriter powerCookie = new FileWriter(baseDir + folderName + fileName);
System.out.println("\tpowe
powerCookie.write(val);
powerCookie.close();
System.out.println("\t"+va
retval = new String("write");
} catch (IOException e) {
System.out.println("Error writing a cookie"); //MESSAGE 4
System.out.println(e.toStr
retval = new String("write err");
}
}
if (task == 3)
{
try {
BufferedReader fileRead = new BufferedReader(new FileReader(baseDir + folderName + fileName));
//the default buffer length crumps after about 2610th character in a string.
StringBuffer strb = new StringBuffer(""); // creates an initially blank StringBuffer
while (fileRead.ready()) // while more remains to be read
strb.append(fileRead.readL
retval = new String(strb); // converts strb to a String
} catch (IOException e) {
System.out.println("Error reading a cookie");
System.out.println(e.toStr
retval = new String("read err");
}
}
if (task == 4)
{
System.out.println("\tDele
File f = new File(baseDir + folderName + fileName);
f.delete();
retval = new String("del");
}
if (task == 5)
{
try {
System.out.println("Starti
File d = new File(baseDir + folderName);
String files[] = null;
files = d.list();
System.out.println("Succes
retval = "";
for (int i = 0; i < files.length; i++) {
if (i !=0) {
retval += "*~&!^H@%#$";
}
retval += files[i].toString();
}
System.out.println(retval)
}
catch (SecurityException e) {
System.out.println("Caught
retval = "list err";
}
}
return retval;
}
}
ASKER
Thanks for a speedy reply! Unfortunately I'm just beginning to understand Java, so I'm going to need your help pasting your code correctly into the existing applet (above). I've tried it myself several different ways, but my java compiler yields 1-2 errors each time.
I also have 3 related follow-up questions:
1. Is importing netscape.security.Privileg
2. Does filewriter automatically create directories/folders if parts of the path you specify don't already exist??
3. Is it customary to include the MS-DOS extension when specifying a filename for filewriter??
Thanks very much for helping me through this, thresh!
Tony
VT
PS Here's what I get when debugging via the Java Console:
# Applet debug level set to 9
# stopApplet: contextID=5 appletID=12971104 parentMWContext=12845312
# Stopping applet: cookin, appletID=12971104, contextID=5
# Applet cookin stopped
# startApplet: contextID=5 appletID=12971104 newFrameMWContext=12845312
# Starting applet: cookin, appletID=12971104, contextID=5
# Applet cookin running
# reflectApplet: contextID=5 appletID=12971104
# reflectApplet: appletID=12971104
# reusing class loader netscape.applet.AppletClas
# context = netscape.applet.MozillaApp
# codebase = file:/F|/WWW/Beaconess/IE/
# scriptOrigin = file:///F|/WWW/Beaconess/I