CGI/Applets for I/O

Ive been searching around for ways to get an applet to read and write to files on the server its being hosted off of, and ive came across a few methods. The easiest one seems to be opening a URL connection to read in a file. But writing a file gets a little bit trickier. You still open a URL connection, but you send data to a cgi script. And the script is the method to which the file is writen.

I found this stuff at:

http://www.webdeveloper.com/java/java_jj_read_write.html

Im having problems getting the writing part to work. If anyone out there could explain how the applet communicates with the CGI Script to allow data transfer, id be much obliged. I don't have much experience with CGI scripts and don't really know what to do.

Thanks, jonathan

[793 byte] By [huangjd] at [2007-9-27 14:42:17]
# 1

There are different ways to transfer data from the applet to the CGI, are you transfering files, binary data, small amounts of text?

It is all HTTP either way, as the CGI is run through the web server, but you need to choose GET, PUT, POST, etc. and application/x-www-form-urlencoded or multipart/form-data

radix_zero at 2007-7-5 22:42:07 > top of Java-index,Other Topics,Java Game Development...
# 2

That's my problem. Ive been reading about that stuff, but don't get it all.

All I will be transfering is small ammounts of text.

the CGI script im using is this:

#!/usr/bin/perl

open(OUT, ">user/cgi-bin/test.txt");

print "content-type: text/plain\n\n";

while (<>) {

print OUT $_;

print $_;

}

close (OUT);

exit 0;

and the method to access it in my applet is:

public void writeFile(String data){

try {

URL url;

URLConnection urlConn;

url = new URL("my url/cgi-bin/write");

urlConn = url.openConnection();

urlConn.setDoInput(true);

urlConn.setDoOutput(true);

urlConn.setUseCaches(false);

urlConn.setRequestProperty("Content-type", "text/plain");

urlConn.setRequestProperty("Content-length", data.length()+"");

PrintStream out = new PrintStream(urlConn.getOutputStream());

gameStats.setText(data);

out.print(data);

out.flush();

out.close();

/*

DataInputStream in = new DataInputStream(urlConn.getInputStream());

String s;

while ((s = in.readLine()) != null)

{ gameStats.setText(s); }

in.close();

this part was giving me security errors, so i commented it out.

*/

}

catch (MalformedURLException mue)

{ gameStats.setText("MUE Exception");}

catch (IOException ioe)

{ gameStats.setText("IOException"); }

}

got any clue what im doing wrong?

huangjd at 2007-7-5 22:42:07 > top of Java-index,Other Topics,Java Game Development...
# 3

I don't do these types of applets very ofter but i just did one the other day so....

check out the HTTP spec at isoc.org or w3c.org is a handy reference (sometimes)

remember that the applet really only comuunicates with the web server, and the cgi only communicates with the web server, applet doesn't communicate directly with the CGI

1. I always specificly set the "Method" header. if the data+url is less than 256 characters using GET is easy (and the default). other wise you need to use POST

2. you are sending data to the webserver of mime-type "text/plain". the webserver should only want for this purpose "application/x-www-form-urlencoded" or "multipart/form-data". basicly of the form KEY=VALUE with all the spaces converted to %20 etc. etc.

3. if you POST the information is placed on the stdin. if you use GET it is place in the environmental variable QUERY_STRING.

so what you need to do is .....

1. get a URLEncoder class. there is one in the J2EE stuff. there are a bunch floating around here and there and how-to java/cgi tutorial should have one.

2. make web page with a form that sends a String to the CGI script and get it working first. then get the applet working.

3. make a cgi script dump all the headers and information it recieves to the stdout. this is handy for debuging and will really help you understand the protocol

unfortunatly i havn't touched perl for a long long time, so you are on your own with how to access the environmental vars. (it is pretty easy if i remember right)

let me know if you have more problems

radix_zero at 2007-7-5 22:42:07 > top of Java-index,Other Topics,Java Game Development...
# 4
Thanks for the help. Im still workin on it, but you gave me a lot of insight on where to look and learn at. Jonathan
huangjd at 2007-7-5 22:42:07 > top of Java-index,Other Topics,Java Game Development...