Hello all,
I am having a terrible time trying to get my
applet and a cgi script to talk to each other properly. First of all, I wrote my own applet and perl script. In the applet I need to retrieve some text from the server that the applet originated from. To make sure that no one else is using the applet currently (and modifying the information in question at the same time as another user) I open a URL connection to a script that checks if a temporary file is present. If it is present it tells the applet that someone else is currently using the data file, if not it creates the temporary file and grants access to the user. Here is the script:
#!/bin/perl
print "Content-type: text/plain\n\n";
($messageIn = <STDIN> );
if($messageIn == "checkFile")
{
$temp = "appletInUse.LCK";
if(-e $temp)
{
print "halt";
}
else
{
print "Go";
open (FILE, ">appletInUse.LCK");
}
}
else
{
print "halt";
}
close(FILE);
exit 0;
And here is the segment of code that corresponds to the script:
URL
test = new URL(CGI_READ_TEST);
display.append(test.toString() + " this is the read url being used.\n");
URLConnection connection = test.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setAllowUserInteraction(false);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("checkFile");
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine = in.readLine();
//If the script says halt then exit
if(inputLine.compareTo("halt") == 0)
{
display.append("Someone else is currently using the system.\nPlease try later.");
//--> write exit method
return;
}
else .......continue method
It gives me a FileNotFound exception at this line when I try to get an input stream from the connection:
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
When I take out the lines where I get an outputstream from the connection, send the string checkFile and the script checks that string as input, and run the program I get no complaints even though I am still opening an input stream. Instead the applet reads in the actual text of the script file that I open a connection to rather than actually runing the script. I searched around for examples of how to do this and found one on sun's
java tutorial site. The address is:
http://www.wi2.uni-erlangen.de/sw/java/tutorial/networking/urls/readingWriting.html Here they are doing the same thing as I am, so I decided to copy their script and application, put there script on my web server, and run the application. I got the same FileNotFound exception at the same place that I got it in my program. I am really getting quite frustrated. I have sat for hours trying to tweak things this way and that, and have come up with nothing. Any suggestions would be more than welcome. Thank you very much.
Cheers,
Greg