Cytael
11-13-2005, 10:02 PM
I'm looking for a way to get an applet to read from/write to a text file on the server...hopefully looking to set up a sort of "guestbookish" thing where a visitor to my website could leave a comment or read comments left by others. I've managed to throw together a java program that has the basic functionality I want locally, but my efforts to come up with a browser-based version aren't turning up a whole lot. The problem, of course, lies in that applets don't have write permission and stuff for obvious security reasons. So I guess what I'm trying to find is some way to either adapt/hack my code to do what I want, or possibly find a way to execute a non-applet java program from a browser? I found an article on this site talking about the former option, but it was written back in 97 and the examples linked to are no longer on the web, and for all my tinkering with that code, I couldn't get it to do anything but laugh at me!
Am I making this way harder than it really is? Should I just give up? Any kind soul feel like giving me a nudge in the right direction?
Anyway, here's the fun parts of the code I'm working with now. Ideally, an applet I'd use would simply contain a call to Notepad(), which would do the rest of the work...or at least that's the approach I'm trying to work out now. Basic idea behind this is a window with a big TextArea in the middle, and 2 textfield/button pairs down at the bottom that either save the TextArea to a specified file or read from a file and put that text in the TextArea (yeah, it'd prolly be super-insecure to add web functionality with open-ended save targets, but I can change that later)
public class Notepad extends JFrame implements ActionListener
{
public static void main(String args[]) { new Notepad(); }
// instance vars omitted
public Notepad()
{
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
IOBox = new Box(BoxLayout.X_AXIS);
saveLocation = new JTextField("save destination");
IOBox.add(saveLocation);
saveMe = new JButton("Save to file");
saveMe.addActionListener(this);
IOBox.add(saveMe);
openLocation = new JTextField("File to be opened");
IOBox.add(openLocation);
openMe = new JButton("Open file");
openMe.addActionListener(this);
IOBox.add(openMe);
cp.add(IOBox, BorderLayout.SOUTH);
// Irrelevant GUI stuff omitted
// There's a JTextArea in here somewhere called theText
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == saveMe)
{
File outputFile = new File(saveLocation.getText());
try{FileWriter outputStream = new FileWriter(outputFile);
outputStream.write(theText.getText());
outputStream.close();
}catch(IOException z){System.err.println("Error writing to " + saveLocation.getText());};
}
else if (source == openMe)
{
try{
File inputFile = new File(openLocation.getText());
FileReader inputStream = new FileReader(inputFile);
String newText = "";
while(inputStream.ready())
{
newText += (char)inputStream.read();
}
theText.setText(newText);
inputStream.close();
}catch(IOException z) {System.err.println("Error reading from " + openLocation.getText());};
}
else System.exit(0);
}
}
Am I making this way harder than it really is? Should I just give up? Any kind soul feel like giving me a nudge in the right direction?
Anyway, here's the fun parts of the code I'm working with now. Ideally, an applet I'd use would simply contain a call to Notepad(), which would do the rest of the work...or at least that's the approach I'm trying to work out now. Basic idea behind this is a window with a big TextArea in the middle, and 2 textfield/button pairs down at the bottom that either save the TextArea to a specified file or read from a file and put that text in the TextArea (yeah, it'd prolly be super-insecure to add web functionality with open-ended save targets, but I can change that later)
public class Notepad extends JFrame implements ActionListener
{
public static void main(String args[]) { new Notepad(); }
// instance vars omitted
public Notepad()
{
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
IOBox = new Box(BoxLayout.X_AXIS);
saveLocation = new JTextField("save destination");
IOBox.add(saveLocation);
saveMe = new JButton("Save to file");
saveMe.addActionListener(this);
IOBox.add(saveMe);
openLocation = new JTextField("File to be opened");
IOBox.add(openLocation);
openMe = new JButton("Open file");
openMe.addActionListener(this);
IOBox.add(openMe);
cp.add(IOBox, BorderLayout.SOUTH);
// Irrelevant GUI stuff omitted
// There's a JTextArea in here somewhere called theText
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == saveMe)
{
File outputFile = new File(saveLocation.getText());
try{FileWriter outputStream = new FileWriter(outputFile);
outputStream.write(theText.getText());
outputStream.close();
}catch(IOException z){System.err.println("Error writing to " + saveLocation.getText());};
}
else if (source == openMe)
{
try{
File inputFile = new File(openLocation.getText());
FileReader inputStream = new FileReader(inputFile);
String newText = "";
while(inputStream.ready())
{
newText += (char)inputStream.read();
}
theText.setText(newText);
inputStream.close();
}catch(IOException z) {System.err.println("Error reading from " + openLocation.getText());};
}
else System.exit(0);
}
}