Lecture 14_ppt

Download Report

Transcript Lecture 14_ppt

Introduction to Programming
the WWW I
CMSC 10100-1
Winter 2003
Lecture 14
Introduction to Perl
• What is Perl?
• How do you run it?
 From command line
 From Web
• How do you write Web pages with it?
Accessing HTML Files Over
the Internet
Your PC
Web Server
(Internet connected)
(Internet connected)
Webbrowser
URL:
www.myserver.com/
funstuff.html
Find
We
b se
rver
at w
ww.
mys
er
ver.c
om
Web server receives
request
Sends file with
funstuff.html
Webbrowser
Here are some fun things
to do:
1. Play baseball.
2. Go swimming.
3. Go to a concert.
ff.html
funstu
e
il
f
e
Th
Web Application Program
• Carries out many dynamic tasks, such as the
following:
 Input a search term, search the WWW, and return the
results
 Calculate and display the number of times that a page
has been viewed
 Verify the input fields on a Web form
 Save a Web form into a database
 Display a special graph, or return the results of a
calculation based on data input from a form
The Common Gateway
Interface
• a standard that enables Web browsers to exchange data
•
with computer programs located on a Web server
first appeared in the NCSA HTTPD Web server software
built by the National Center for Super-computing
Applications (NCSA).
 one of the first widely used Web servers
 was simple and the program source code was made available for
free
• It is simple to use and available on a variety of Web servers.
How Browsers and Web
Applications Work with CGI
An Interface with Different
Programming Languages
• Web application programs that are
•
developed specifically to work with the CGI
standard are known as CGI programs.
Lots of different programming languages
can be used. For example,






Perl,
Visual Basic,
Java,
C,
C++,
and UNIX shell scripts.
The Perl Programming
Language
• Practical Extension and Reporting
Language
 invented in 1987 by Larry Wall at NASA’s Jet
Propulsion Laboratory
 developed as a utility programming language
for the UNIX operating system
 gained popularity because of its ease of use,
free availability via the Internet, and its powerful
combination of features
Why Perl is Popular
• Perl is a free language with lots of free
applications
• Perl is easier to work with than many
other languages
• Perl provides a CGI interface module
• Perl applications are portable.
Internet Service Provider Issues
• Some things to determine with your ISP
 Allow CGI programs to execute on its Webserver? Does it have Perl? What Version?
 Login and initial password on the Web server?
 Where do you put your Perl programs on the Web
server and what permission settings?
 How much disk space for publishing?
 Support FTP and/or Telnet access? or SSH/SCP?
Summary
• Web pages written in HTML are static and
cannot interact with users.
• CGI is an interface standard that allows
computer programs to communicate with
Web servers. Several programming
languages can be used with CGI.
What You Need to Get Started
•
•
•
•
SCP/SSH
Connecting to the Web server
Setting up your directories.
Getting the location of the Perl interpreter.
Navigating UNIX Directories
/
bin
etc
Some UNIX system
directories
usr
...
jsmith
/ - Operating system files
home
...
/home - ISP User directories
perlpgm
/home/jsmith jsmith's files and directories.
/home/perlpgm My files
and directories
Navigating UNIX Directories 2
/home/perlpgm
/home/perlpgm My files and directories.
data
/home/perlpgm/data A directory I place data I want on
the server but not on the Internet.
perl-pgm-www
/home/perlpgm/perl-pgm-www Where I need to place my HTML files.
cgi-bin
/home/perlpgm/perl-pgm-www/cgi-bin
- Where I need to place my web application
programs.
Finding The Location Of Perl
• Perl interpreter
 A program that translates Perl program commands
into commands that are understandable to a
computer.
 Runs your Perl programs and generates any
output.
 Its command name is simply perl.
 It can be installed in any of several places on a
Web server.
Finding Location Of Perl
• SSH onto Web Server enter:
 which perl
 where is perl
Perl Interpreter
Location
Starting Your Program
Development Process
• Each time you develop and run a program:
 Create a program file and copy (or save) it into the
correct directory.
 Change your program’s access permissions.
 Check your program’s syntax.
 Run your program.
Create Your Program File
• Editors are computer applications that enable
you to create, change, and save files
 Microsoft Windows, Notepad is a simple editor
that works well for Perl development.
 On UNIX systems, the Pico, Vi, and Emacs editors
are popular choices.
Starting Your First Program
• Start Editor and enter the following:
#!/usr/local/bin/perl
# This program prints out a
simple message
print “Steady Plodding Brings Prosperity\n”;
Program Entered in Pico
Run The Program
• Save the program file on the web server.
• Enter the full path to the program file to run.
• For example:
/home/kirby/html/cgi-bin/simple1.cgi
Home
Directory
Directories
On Web
Server
Program File
Permissions On A Unix Web
Server
• UNIX access permissions are used to define
the access rights of your files
 read permissions define if the file can be read
 write permissions define if the file can be changed,
 execute permissions define if the file can be
executed as a program
• You set access permissions for your user ID,
your user ID’s group, and everyone else
To Check Your Program Syntax
(on a UNIX Web Server)
• establish an SSH session,
• navigate to the directory that contains the file,
• enter perl –c filename, where filename is the
program file whose syntax you want to. For example,
• cd html/cgi-bin
• perl –c simple1.cgi
• If no syntax errors then you will seed:
• simple1.cgi syntax OK
Program with Syntax errors
Missing
quote
mark
Running Your Program
• At least two different ways to run your Perl
programs:
 Directly on a Web server or PC without a
browser
 Using your browser over the Internet.
Getting Ready to Run Your
Program Over the Internet
1. To use a browser over the Internet, add the
following MIME content-type line:
print “Content-type: text/html\n\n”;.
#!/usr/local/bin/perl
print “Content-type: text/html\n\n”;
# This program prints out a simple message
print “Steady Plodding Brings Prosperity\n”;
Change Program Process
1. Edit the program
2. Change the program.
3. Save the file.
4. Check the program’s syntax.
5. Run the program.
Running Your Program Over the
Internet
1. Connect to the Internet.
2. Start your browser.
3. Enter the URL or Web address to your file
4. Check the program’s syntax.
5. Run the program.
For example, assume saved the in a file called
simple2.cgi in my cgi-bin directory on the Web
server. Can execute by the following:
http://people.cs.uchicago.edu/~kirby/cgi-bin/simple2.cgi
Output Of Program Executed Over
the Internet
Dealing with Problems
• Many Web servers redirect the errors from
CGI programs into a separate error log
located on the server.
 You may receive a generic, cryptic message
when running program’s with errors.
 Two common messages are Internal Server
Error (Figure 2.17) and 500 Server Error.
Some Things to Check
•
•
•
•
•
•
•
Verify the program syntax.
Verify the access permission.
Verify the file has the proper extension (.pl or .cgi).
Verify the program is stored in the correct directory.
Verify the correct Web address to your program.
Verify the first line has the correct of the Perl interpreter.
Confirm the accuracy of your MIME Content-type line.
An Internet Server Error
Generating HTML Statement
from Perl Programs
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<HTML> <HEAD> <TITLE> Example </TITLE></HEAD>";
print "<BODY>";
print "<B><Font Size=5>This is a Test </FONT></B>";
print "A very Interesting test";
print "</BODY></HTML>";
Program Output
Another approach:
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print<<END;
<HTML>
<HEAD>
<TITLE>Example</TITLE>
</HEAD>
<BODY>
<B><Font Size=5>This is a Test </FONT></B>
A very Interesting test
</BODY></HTML>
END
What is this?
• Everything between ENDs gets treated as
if it were inside a double quote
• Use this with “require” to print out a page
with a fixed header, footer, body
• Use require “foo.pl”; to execute the
code in that file in that spot
Summary
• There are several different configurations you can use to
develop CGI/Perl programs.
 Using FTP and Telent are common
• Steps to create a program: create with editor, enter
•
program, set permissions, check syntax, and run the
program.
Two statements are required:
 First line identifies Perl interpreter location.
 Second line specifies the MIME Content-type.