Transcript PZ15A

PZ15A - The Internet
Programming Language Design and Implementation (4th Edition)
by T. Pratt and M. Zelkowitz
Prentice Hall, 2001
Section 12.2.2-12.2.4
PZ15A
Programming Language design and Implementation -4th Edition
Copyright©Prentice Hall, 2000
1
HTML FORMS
A method for passing information between a user at a
Web browser and a Web server.
This information is passed to a program on the server
system. This is the Common Gateway Interface (CGI)
file.
Web servers typically have a specific cgi-bin directory
containing these CGI programs.
HTML forms are used:
<form method=“type”
action= “location of cgi script to execute”>
text
</form>
Perl often used as language for such scripts
PZ15A
Programming Language design and Implementation -4th Edition
Copyright©Prentice Hall, 2000
2
CGI script example
PZ15A
Programming Language design and Implementation -4th Edition
Copyright©Prentice Hall, 2000
3
HTML for form
<HTML>
<HEAD>
<TITLE>cgi-test</TITLE>
</HEAD>
<BODY>
<p> This is a sample page to read
two data items from the web page:
<form action="cgi-bin/xaction" method=get>
<p>First name=<input type=text name=xfirst size=10>
<br>Last name=<input type=text name=xlast size=20>
<br> <input type=submit value=SEND>
<input type=reset value=RESET>
</form>
Parameters passed as arguments
</BODY>
xfirst and xlast
</HTML>
PZ15A
Programming Language design and Implementation -4th Edition
Copyright©Prentice Hall, 2000
4
Perl - CGI script
#!/usr/bin/perl
print “Content-Type: text/html\n\n”;
print “<html><head>\n”;
print “<title>Sample PERL script</title>\n”;
print “</head><body>\n”;
print “<p>Query_string is $ENV{'QUERY_STRING'}\n”;
foreach ( split( /&/, $ENV{'QUERY_STRING'}) )
{ ( $key, $val ) = split( /=/, $_, 2 );
$tmp{$key} = $val; }
print “<p>First name is <b>$tmp{'xfirst'}</b>\n”;
print “<p>Last name is <b>$tmp{'xlast'}</b>\n”;
print “</body></html>\n”
• Perl program first reads parameters as xfirst&zlast
from $ENV (environment) into QUERY_STRING
• Output of Perl is the syntax of an HTML page that is
displayed
PZ15A
Programming Language design and Implementation -4th Edition
Copyright©Prentice Hall, 2000
5
Java applets
Issue: Many different protocols
Solution: Send program to read protocol as part of HTML
(Role for Java)
Moves processing needs from HTML server to HTML cllient
Do more processing on client side of web
import java.awt.*; /* applet library */
public class hello extends java.applet.Applet
public void paint(Graphics x)
{x.drawString(“Hello World”, 100, 100);}
Displayed by:
<html>
<body>
<applet code = “hello.class” width=200 height=200>
</applet>
</body>
</html>
PZ15A
Programming Language design and Implementation -4th Edition
Copyright©Prentice Hall, 2000
6