cgi programming using apache. concepts browser prepares parameter list list is attached to name of...

11
CGI programming Using Apache

Upload: sherman-sims

Post on 23-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

CGI programmingUsing Apache

Page 2: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Concepts

• Browser • prepares parameter list• List is attached to name of program to run on server• "submit" button sends string to server as name/value pairs• Server locates program in CGI directory• Server loads program• Server passes string to program• Program processes parameters and acts as needed• Program creates strings and outputs to server

• One line of HTML code per output statement (cout, printf, etc)• ALL stream output goes to the Server

• Server sends strings to browser• Browser interprets the HTML code & displays the "page"

Page 3: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

CGI output (C++ example)

#include <iomanip>cout << "Content-Type: text/html\n\n" << endl;cout << "<!DOCTYPE html>" << endl; // HTML 5cout << "<html>" << endl;…. Your HTML & text go here …cout << "</html>" << endl;• 1st line identifies output media type to browser• 2nd line identifies document type version• 3rd line is actual start of the page

Page 4: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Content-type problems

• Inability of data source to identify content• Wrong file extension• Bad file extension• Extension collisions• Ambiguous container formats• Ambiguous magic numbers

• Inability of receiver to trust sender's media type• Programmers try to guess content type by examination

Page 5: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Using styles

• Output is a stream representing a single "file"• HTML files do NOT have to have one tag per line• Output can be individual lines or several long lines:

cout<< "<p> text <ol><li>item1</li></ol>"<<endl;• Styles can be embedded or inline• Generate style tags just like any other HTMLcout << "<style> .red {font:red} " << endl;cout << ".bigtext {text-size:150%} " << endl; </style>" << endl;

Page 6: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Using JavaScript

• JS can be inserted:• as functions in <head> </head>• As inline code inside <script></script> in body of doc

• CGI generated pages "act like" real pages

Page 7: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Getting to your server• Connecting to the TJW machine

• Go to SSL>BINGHAMTON>EDU and download the MTN3270 (Mocha) program• Edit the properties of mtn3270 to be sure you are going to bingTJW, not bingTZ1.

• Login to the TJW mainframe using MTN3270• Your 3270 userid & password are on my Grade Tracker• Follow the prompts then type: #CP DISC" and your session will disconnect. Close the window.

• Login to TJWnnn.cc.Binghamton.edu using Putty or SSH• The userid & password will ONLY be given to you in class.• You can now use the editor named “nano” to edit files.

• To work in full GUI mode, type: vncserver• It will prompt you for a NEW password. Make one up. This will be for your VNC client to connect

(for a 3rd time) to your system. I recommend the freeware program called “tightvnc”. You can now close Putty.

• Download & install your VNC client, then use it to login in GUI mode

Page 8: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Navigating a website• Do I have to install Apache on my PC?

• No. The Apache server is only on the server (TJW) and is already installed.• Here is the data flow:1. User has your form “somewhere”. It could be a simple file on their hard drive or in the HTML folder of

your webserver machine (TJW in this case).2. User completes the form and clicks “submit”3. The “action=” attribute of the FORM tag tells their browser to gather the input fields and send them to

wherever the “action” attribute says to send them4. The TJW Apache server sees the incoming request for your CGI program. The program MUST be in

var/www/cgi-bin directory (after you compile it on TJW and move the executable-output (a.out) there from your home directory.) You SHOULD rename a.out to something more meaningful and that is the name to use in your “action=” attribute.

5. The Apache server loads the program and passes your input fields as command-line arguments, just like any program started form a command-line. Your program gets them via:void main(int argc, char*argv[]) {}

6. Your program gets the data, processes it and outputs a webpage as we discussed in class. The output is sent by the Apache server to the user’s browser which then displays the page you built with the results.

Page 9: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Activating your CGI program• Generate an HTML form (using cout or printf):<form name='myform' action="cgi-bin/your program's executable" method='post' ><!-- for testing: use action='mailto:your email address' -->… {your JS function def's and html and go here} document.myform.submit(); // this submits the form to the browser<td><input onclick='chkflds();' type='button' value="multiply them!"></TD></form>

• Where I have "chkflds" you could put the name of a function to validate the fields• The function would have to be inside <script> and <head> tags• RECOMMENDATION: put the form's elements inside a table• <form> <table> .. Input/button tags here… </table></form>

Page 10: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Setting up your Apache server• Open terminal window• Type:

nano /etc/httpd/conf/httpd.conf(or use your favorite editor instead of nano)• Locate "ServerAdmin" and replace the email address with yours • Locate "ServerName" and replace the default with your TJWnnn account

(tjwnnn.cc.binghamton.edu)• Save the file using the same name it had• In the terminal window type the following command:

apachectl start• Close your session

Page 11: CGI programming Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string

Testing your web server

• Create a simple web page• Save it in /var/www/html with the name: index.html• Open a browser on your own PC (NOT on the TJW machine)• Enter the address of your tjw account

e.g.; tjw241.cc.binghamton.edu (that's mine!!)• You should now see your test page