Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cgi applet communication problems

 
Gregory Banks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Holloway
Saloon Keeper
Posts: 18366
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a bit much to make sense of without printing out, but one thing you might try is to set the HTTP status code if the file's in use. Then, instead of attempting to read and decode a "file unavailable" message, you could just test the status code and bypass the reading operation.
 
Gregory Banks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I can't really do that though because that would just be avoiding the problem, and I have to write to a file anyway so I have to face it sometime. I think after exhausting everything that I can think of tweaking I have decided that it is the web server's fault. Our web server seems to be so patched together that many things do not work quite as they should and my applet, I think, is one of them. I have read many things that say the URL and URLConnection classes are somewhat flaky and cannot be expected to really work the first time anyway, and on some servers it will not work at all. So, some other people have installed apache web server software on an ultra 5 in the building and I think I am going to try it on that. Does anyone know anything about setting up the correct environment for using the URL class to talk to a cgi script on the server side to have it write/read information. Any help would be greatly appreciated. Thanks.
Cheers,
Greg
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic