Forum Moderators: coopster & phranque

Message Too Old, No Replies

         

Zoggo

4:50 pm on Apr 17, 2003 (gmt 0)



Hi, I am trying to get an applet to write a file to my web server. The applet sends the data, but I get no response. The perl I am using is :

#! /usr/bin/perl
######################################################################
# This program allows a java Applet to write direct to a web server #
# It is based on an article "Java Jive" by Scott Clark #
######################################################################

print "content-type: text/plain\n\n";
# print content type header top the browser

open(OUT,">>111111.TST");
print OUT ">>>>>> <<<<<<<<";
# open the file 111111.TST for writing (move "cursor" to start)
# (>> means append to file)
while(<>){ # I don't know which file handle is being read?
print OUT $_; # print what is read from the file handle to the file
# referenced by the handle OUT
print $_;
}
print OUT "Has this been successful?";
print "Has this been successful?";
close(OUT);
#close the filehandle
exit 0;
#exit

I am very new to perl, how can I identify what is being received from the applet, and why the file is not being written? The applet sends this header when called the perl script :

data="POST \\user\\htdocs\\cgi-bin\\ResUpd.pl HTTP/1.0\r\nContent-type: text/plain\r\n"+
"Content-length: "+data.length()+"\r\n\r\n"+data;

Can anyone point me in the right direction?

regards
KPJ

ShawnR

2:50 pm on Apr 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi KPJ, and welcome to WebmasterWorld!

I can't see anything obviously wrong with your code. The problem could range from a bug in your script, an error in the way your applet tries to call it, a permission error with opening the file, etc. I suggest you try debugging by testing different stages. For example:

  • Print a debug alert statement before the POST, to ensure that you are sending what you think you are sending.
  • Point your browser to [localhost...] (where filename.pl is the name of the file containing the perl script, and it is stored in cgi-bin). See if the file gets created, with ">>>>>> <<<<<<<<", and you get back a web page saying "Has this been successful?"
  • If not, comment out all the lines except the following two:
    print "content-type: text/HTML\n\n";
    print "<br>Has this been successful?";
    Then try point your web browser again...

A couple of questions and comments:

  • "\\user\\htdocs\\cgi-bin\\ResUpd.pl" Is that correct? I would have thought "/user/htdocs/cgi-bin/ResUpd.pl"
  • You have a comment "# I don't know which file handle is being read?". <> reads from each succesive filename in the argument list, but you don't call the program with a list of filenames as arguments, so that could be the problem.
  • Your code should check the 'open' was successful, and call an error handling return if not.
  • You have a bunch of comments in your code which make it hard to read. Either you copied these from the article, or you developed the habit because that is how some universities teach. It's not really a big deal in such a small bit of code, but... it is not a good habit! (For example, comments like: #close the filehandle (Just after the statement close(OUT); ) or #exit (just after the statement exit 0; ). It is certainly a good habit to comment code well, but stating the obvious just makes your code hard to read and hides the really significant comments. Also, it traps you, the inexperienced programmer into believing that the line of code is correct, so you don't really look at it critically when debugging (Your brain just looks at the comment and assumes the code does what the comment says)

Sorry if the above is not much help. Post again once you have the results from trying some of the above debugging.

Shawn