Lecture 12a: Web
Servers and CGI
What You Will Learn Today
- Explain the purpose and types of web server software.
- Explain the function of server-side scripts
and CGI scripts.
- Describe the Perl scripting language and its use in
CGI scripting.
- Describe Java Server Pages,
Java Beans and Servlets.
Web Server Software
- Web server software is runs on a server computer to provide access
to web sites.
- When the client's browser requests a page, the server sends the page and
any required files e.g. images.
- Apache is the most popular web server software. It is free and runs
on UNIX/Linux computers among others.
- Microsoft Internet Information Services (IIS) and Personal Web
Server are also popular and relatively easy to use.
- Client-side scripts are downloaded to and run on the client's
computer.
- Server-side scripts are stored and run on the server and access
server data, often in databases.
- Different servers have built-in or added-on support for different
scripting languages.
- Languages include Java
Server Pages (JSP), Microsoft's Active Server Pages (ASP), and PHP.
- The script language type is denoted by the extension (.jsp, .php, .asp,
etc.).
- The scripts can be embedded in HTML.
- The server replaces them with text (e.g. HTML tags) before sending
the result to the client's browser.
CGI Scripts
- Common Gateway Interface (CGI) is a protocol for processing information sent to the server
through an HTML form.
- The form tag's action and method attributes determine the script to
be used and the GET or POST protocol the script will use.
<form action="timezone.jsp" method="POST">
- The browser displays the results of these scripts the same as it would for
a static HTML page.
- Many languages can be used to process this information, including Java
Server Pages, C, Perl and shell scripts.
- CGI scripts are normally stored in a different folder on the web server
than static HTML pages.
- On Apache, by default, CGI scripts are in cgi-bin and HTML
documents are in htdocs.
- Other directories include src (for the source code), conf
(for server setup/configuration) and logs (for records of server
access).
- CGI scripts must be executable programs.
chmod +x scriptname
- Perl is a powerful interpreted scripting language commonly used for CGI scripting,
especially in the mid-1990s.
- It is similar to shell scripting languages like sh and programming
languages like C.
- Its strength is its concise syntax and ability to process text.
- Perl scripts generally have a .pl extension and begin with a line
indicating the path to the Perl interpreter.
- Here is a simple script
hello.pl
that can be run from the command line.
#!/usr/bin/perl
print "Hello, world!\n";
- Here is a script
date.pl
that can be used for creating a web
page.
#!/usr/bin/perl
$date = `date`;
print qq{Content-type: text/html\n\n<html>
<head><title> Hello world! </title></head>
<body>
<h1> Hello world! </h1>
<p> The current time on the server is: $date </p>
</body>
</html>
};
- Here is a script
printenv.pl
that prints the environment
variables of the server.
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
while (($key, $val) = each %ENV) {
print "$key = $val<BR>\n";
}
- To use Java Server Pages, install a JSP container on your web server such
as the free Apache Tomcat server.
- Here is a script
date.jsp
to be put in a subdirectory of the
webapps subdirectory.
<html>
<head><title>Date JSP</title></head>
<body>
<h1>Date JSP</h1>
<p>The current time on the server is:
<%= new java.util.Date() %>
</p>
</body>
</html>
- Test by opening the file in a browser, e.g.
http://hostname/subdirectory/date.jsp
- JSP can be used with other technologies like Java Beans and servlets.
- Java Beans are Java classes that carry out computations.
- They have default constructors and
getPropertyName()
and
setPropertyName()
methods.
- They can be accessed through
<jsp:getProperty />
and <jsp:setProperty
/>
tags in JSP pages.
<jsp:setProperty name="zone" property="city" param="city" />
- Servlets are Java programs that carry out computations and can emit
HTML code.
- Servlets extend the HttpServlet class.
- JSP pages are automatically translated into servlets.
-
Project 5 and
Lab 11 are due Friday. Please complete
all coursework by next Friday.
-
The final project is due the day of the scheduled final assessment.
-
Readings: Hofstetter ch. 22; Horstmann Ch. 25 (JSP, Java Beans,
Servlets)
-
Electronic References: HTML 4 17.13 (form submission), Mastering the
World Wide Web: class notes 1-4; Stein examples: Ch. 8, 9