CGI Programming UNIT 9
CGI Programming UNIT 9
CGI Programming UNIT 9
UNIT-8
CGI Programming
Eduoncloud.com
Page 1
UNIT-8
CGI Programming
Java servlets and JSP (Java Server Pages), which are similar to ASP and
somewhat analogous to Java applets.
Running CGI
In most cases, the output is an XHTML document, with the server writing
most of the header.
Eduoncloud.com
Page 2
UNIT-8
CGI Programming
Running a Perl CGI program requires that perl, the Perl interpreter be
called. For UNIX or Linux, that requires the line
#!/usr/bin/perl
be include at the beginning of the file. (This isnt necessary for
Windows-based servers.)
</a>
A form can invoke a Perl CGI program by giving its address in the
action attribute of the form tag:
<form action = "/cgi-bin/popcorn.cgi
method = "post" >
Eduoncloud.com
Page 3
UNIT-8
CGI Programming
reply.html
<!-- to call a simple Perl CGI program -->
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
reply.cgi
#!/usr/bin/perl
Eduoncloud.com
Page 4
CGI Programming
UNIT-8
--8
What is CGI? How parsing of data is done for GET and POST
methods. --10
CGI Definition:
CGI is a Common Gateway Interface for communication between
the web server and CGI scripts that can be run on the server.
Query Strings
query strings can be handwritten and used directly with GET and
POST
Eduoncloud.com
Page 5
UNIT-8
CGI Programming
For every element (widget) with a value, the name and value is
coded as a character string in the format of name = value and
included in the query string:
payment = discover
If there is more than one widget, they are separated by ampersands (&)
Caramel=7&payment=discover
Special characters can be coded using a percent sign and their ASCII
value ( %20 for space and %21 for !)
Payment=visa & saying=Eat %20your %20fruit % 21
get Method
The action of a form is always get or post, with get being the default.
When get is used, the browser attaches the query string to the URL
of the CGI program, so the form data is transmitted to the server
with the URL.
The server removes the query string from URL and places it in the
environmental variable QUERY_STRING, where program can access it.
post Method
The post method passes the query string through standard input to
the CGI program so the CGI programs just reads it as STDIN.
Eduoncloud.com
Page 6
UNIT-8
CGI Programming
There is no length limitation for the query string with the post
methods, so its a better choice if there are several widgets.
$request_method = $ENV{REQUEST_METHOD};
if ( $request_method eq GET )
{
$query_string = $ENV{QUERY_STRING};
}
elsif ( $request_method eq POST )
{
read( STDIN, $query_string,
$ENV{CONTENT_LENGTH} );
}
else
{
print Error = the request method is illegal \n;
exit(1);
}
--06
Purpose:
With the Perl CGI.pm module, you can create dynamic Web
pages quickly and easily from within the powerful Perl
programming language.
CGI.pm contain functions to perform common tasks like
writing commonly used HTML tags and for creating form
elements such as buttons and lists.
Following declaration gives access to a particular module of
CGI.pm
use CGI ":standard";
Eduoncloud.com
Page 7
UNIT-8
CGI Programming
br
returns a break tag <br />
print br;
h1
places the string in a largest size header
Example
textarea
places a textarea in the HXTML with the attributes passed as a hash literal
Example:
print textarea( -name => "Description",
- rows => "2",
-cols => "35" );
produces:
<textarea name = "Description" rows = "2" cols = "35" >
</textarea>
a
creates a hypertext link or another anchor
print ( a { -href => "fruit.html" },
"Press here for fruit descriptions" );
produces
<a href = "fruit.html" >
Press here for fruit descriptions
</a>
Eduoncloud.com
Page 8
UNIT-8
CGI Programming
ol and li
ol creates an ordered list:
print ol ( li ( {-type =>"square"}, ["milk", "bread", "cheese"] ) );
produces
<ol>
<li type="square"> milk </li>
<li type="square"> bread </li>
<li type="square"> cheese </li>
</ol>
radio_group
sets up an entire radio group
print radio_group ( -name=>"colors",
-value => ['blue', 'green", 'yellow', 'red'],
-default => 'blue');
produces
<input
<input
<input
<input
----------------------------------start_html
produces the boilerplate for the opening HTML;
print start_html("Paul's Gardening Supplies");
produces
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
Eduoncloud.com
Page 9
UNIT-8
CGI Programming
header
produces boilerplate for the beginning of an XHTML document
print header
produces
Content-type: text/html; charset=ISO-8859-1
blank line
param
allows the program separate name/value pairs that appear in a query
string.
If the query string is
name=Bob%20Brumbel&payment=discover
The statement
my $myName = param("name")
assigns the variable $myName the value "Bob Brumbel"
The statement
my ($myName, $salary) = ( param("name"),
param("payment") );
would assign "Bob Brumbel" to $myName and "discover" to
$salary
Eduoncloud.com
Page 10
UNIT-8
CGI Programming
end_html
produces closing tags for an XHTML document
print end_html;
Produces :
</body> </html>
--10
session that the next session can use, despite the fact that this
information may be useful.
Eduoncloud.com
Page 11
UNIT-8
CGI Programming
What Is A Cookie?
Definition:
A cookie is a small object of information used to maintain
state information between the web server and the client browser. .
Every browser includes all the cookies that the host machine has
stored that are associated with the server.
Cookies are stored as text and can be deleted by the host machine.
Creating Cookies
To display all the cookies (both names and values), we could write:
print "Cookie Name \t Cookie Value <br />;
foreach $name( keys cookie() )
{
print "$name \t cookie($name) <br />";
}
Eduoncloud.com
Page 12
UNIT-8
CGI Programming
Write perl program which uses built-in function to get current system time
and date and formats that information and print on screen. --10
If we wanted the date and time, we could use time to provide the
seconds since January 1, 1970.
time_date.pl
# Input: None
# Output: The nine values return by localtime
($sec, $min, $hour, $mday, $mon, $year, $wday,
$yday, $iddat) = localtime;
-----------------------------------------------------------------------
Eduoncloud.com
Page 13
UNIT-8
CGI Programming
$day_of_week =
( qw( Sunday Monday
$day_of_month = (localtime)[3];
#>>> Get the day of a month
$day_cookie = cookie(
Page 14
UNIT-8
CGI Programming
#>>> start Producing the return document, First, put the cookie in the new header
if (scalar(@last_day) == 0)
{
print "Welcome to you on your first visit to our
site <br />";
} # Otherwise, welcome the uyser back and give the date of the last visit
else
{
($day_of_week, $day_of_month, $month) = @last_day;
print "Welcome back! <br />"
print "Your last visit was on "
print "$day_of_week, $month $day_of_month<br />";
}
print end_html;
Eduoncloud.com
Page 15