Click to See Complete Forum and Search --> : Applet I/O?


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);

}
}

Waylander
11-14-2005, 12:39 AM
You have the right idea but just slightly in the wrong direction.

A simpler way to achieve your goal would be a simple html form, with your two buttons and text field. The buttons would then post thier data to a server side script which handles the interaction for how you want store the information.

You can just use a text file and have your script append the data into the file, if your set on using java then you can make your scripts with JSP (Java Server Pages) which is a good techonology or you can use PHP which is generally considered to be easier.

You could also then create a PHP or a JSP that displays the comments that you have recieved. If you wanted to get further into it you could use a Database or an XML file to store the data. Buntine made a guestbook system with XML and ASP which is pretty good, not much for .net myself, XML and JSP be my choice.

I would recommend starting of with a .txt file and a couple jsp scripts and seeing how you go, If you have any problems along the way just come back to the thread and the peeps here will help you out :)

Waylander.

Cytael
12-20-2005, 02:53 PM
So it's like a month after my original post here, but I finally got around to learning a little PHP and found creating this thing to be a lot easier than I thought it might be. Anywho, thanks for the pointers, Waylander! :)

Waylander
12-20-2005, 06:02 PM
yeah, no worries man.

Waylander.