Dynamic Sites & Basic CGI: Studio Exercise ( 9 February 2005)
We need to start this exercise by making sure that you are able to install and run simple CGI programs on Grace written in Perl.
Setup: Create a subdirectory in your 'www/409' directory on grace called "cgi". Remember to set the permissions so that the web server can read and execute both the contents _and_ the program files. (Example: chmod 755 first.cgi)
Create the Program File: Using a text editor type in the following program, _exactly_ as it appears below:
Install the Program File: If you created it on your PC or Mac, now upload it to your cgi subdirectory. Make sure you transfer the file in "ASCII" or "Text" mode and not "binary." Then login to grace and view the file using a Unix text editor (like pico or vi). Make sure it looks okay (are the line endings in the right place? are there strange characters? if there are problems, you may need to save it in a different format, or upload it differently...)
What should the output of this program be? (Describe it in your blog entry?)
Make sure you understand what the program is supposed to be doing, so you will recognize correct output when you see it.
Test the Program File: Now, run the program directly on Grace, by typing this command at the prompt:
perl first.cgi
If all goes well, your program should execute. If it doesn't, track down and fix the syntax errors until it runs.
Capture the output: To more accurately simulate how the web server will execute your program, use redirection of output to capture the output of your program into a text file. Try running your program a slightly different way:
first.cgi > firstout.txt
Notice we don't _have_ to use the "Perl" command at the command line.
Examine the output to make sure it is formed correctly by viewing the file firstout.txt. If everything looks okay, keep going. If not, see if you can find the problem.
Question to answer in your blog entry: What precedes the html in your output? Why would that need to be there?
Now you are ready to see if the program can be run as a cgi program.
Note: on some systems the program must end in a .cgi suffix regardless of the language in which it was written, in order to run as a cgi program. Grace is one of them. Some servers also require that all cgi files be placed in a "cgi-bin" directory in your main web directory, but Grace does not require this; you can place .cgi files anywhere in your web directory.
Test your cgi program by loading it in a browser (http://www.rit.edu/~yourid/409/cgi/first.cgi).
Create Another CGI Program: If that works, create a second cgi program--call it second.cgi. This second program will not produce a web page. All that it will do is output an http response header to send the user to a new location. Not surprisingly, this uses a Location: header, and works much like a html refresh meta-tag. It tells the browser to request another document at another location. Have the program redirect to your 409 personal page. (http://www.rit.edu/~yourid/409/).
Here's an example of a Location header:
Location: http://www.w3.org/pub/WWW/People.html
How would you output this? You use a print statement to output this type of header, of course. The header is for the browser and tells it to look elsewhere for a document. Remember to output a blank line after the header! There is no html output by this program and that means, you do not return content from your program (so what header don't you need?).
Test the Second Program: Create an index.html file for your cgi directory, and include a link to both cgi programs. When a user clicks on the link to the second.cgi program, s/he should be redirected immediately to your main 409 page.
Question to answer on your blog: Is this a useful thing? Why bother?
Excerpt: We did basic CGI exercises in class. See mine: http://www.rit.edu/~kps8191/cgi/first.cgi http://www.rit.edu/~kps8191/cgi/second.cgi For the first CGI program, the output was exactly as expected: a simple HTML website. The first print command told the s...
Read More: CGI
Excerpt: Today in class we created our first cgi scripts. The first one simply outputs a simple web page with some text, and the second script redirects the user to my 409 home page through the use of a location header....
Read More: Dynamic Sites & Basic CGI
Excerpt: Today Today in class we did a little bit of basic CGI stuff. I already had a cgi-bin directory created and had the FormMail script implemented for a survey I had to give people for a research project earlier in...
Read More: 20050207 inclass (CGI)
Excerpt: Today we did some basic CGI tutorials. For my first.cgi, I expect the output to be a very simple webpage, with a title, a Top heading and a sentence for the content. There is a line in my script that...
Read More: CGI'ing it Up
Excerpt: In class today we did some cgi exercises. You can see mine here....
Read More: Dynamic Sites & Basic CGI
Excerpt: Examples of CGI Scripts
Read More: CGI Scripts
Excerpt: Looking at the code for CGI script it should print out the html code for a website with "Content Type: " at the top followed by a blank line then "Generic Web Page" followed by a horizontal rule then the...
Read More: Dynamic Sites & Basic CGI
Excerpt: Today in class we worked on CGI exercises. First we created a simple script that displayed a webpage. Second, we created a script that would redirect the page to another site. You can see those examples here....
Read More: CGI Exercises
Excerpt: Ah Ha! So thats how you do it... After Many attemps and silly errors, with the prof. watching over me, I finally got the dumb thing to work....
Read More: Ah...so thats how its done...
Excerpt: My expected results from the CGI script is a simple web page, with a few horizontal rules and "stuff goes here" with a heading of Generic Web Page. Fairly simple. Seems to work: enjoy the madness As far as the...
Read More: CGI happiness
Excerpt: Last class's exercise was to create a cgi script to essentialy reirect you to my 409 page. Here is the finished exercise. Here is more on the assignment....
Read More: CGI Stuff pt.1
Excerpt: this excersie was fun. I see alot I can use for my wifes web page. The email form program will be a perfect addition to the page...
Read More: Dynamic Sites and CGI
Excerpt: The first thing I did in lab was test out to see if I could run cgi. The output of the entry was a webpage in HTML text format. I don't know why the HTML tag was preceeded by what...
Read More: Jcaligaris lab (2/9) First Part
Excerpt: This week's studio exercises...
Read More: Dynamic Sites and Basic CGI
Excerpt: Once again, I forgot to post to my blog last week. Last wednesday we did some cgi exercises. The first takes you to a generic webpage. The second is used to redirect you. And the third is a form where...
Read More: CGI Exercises
Excerpt: The title says it all. Yesterday and today we worked with CGI scripts. You can check out my FormMail CGI here....
Read More: CGI pwns you.
Excerpt: So it broke again but i think i've managed to fix it again. OK so i've got a weblog running again. The old one is here and all the new stuff will be here! Woot! First and Second .cgi are...
Read More: http://www.rit.edu/~rwt1777/woot/archives/2005/02/so_it_broke_aga.html
i don't know if this was intended or not, but you are missing a line break after in the perl code :)
Posted by: Kevin Sweeney on January 27, 2005 01:52 AM | Permalink to Comment
(Trackback link: http://www.it.rit.edu/~ell/mt/mt-tb.cgi/1233)