8. Server-side design
8.1 Introduction8.1.1 Server-side processingWe use server-side processing under two related and yet different situations:
To process data on a web server, we often use a method known as CGI (Common Gateway Interface). CGI is a standard through which an information server (such as a web server) and external programs/scripts interact with each other . CGI is not a programming language but a standard for data communication between the web server and external programs. The following figure provides a schematic illustration of the entire process: 8.1.2 Advantages and disadvantages8.1.2.1 Advantages
8.1.2.2 Disadvantage
8.1.3 When to use server-side processing rather than client-side processingWhen the same function can be realized using either server-side or client-side processing, you will have to use server-side processing
8.1.4 How can a web server tell when to enable server-side processingThere are two methods to let a web server know when to enable server-side processing: 8.1.4.1 File suffix association Each webpage is stored on a web server as a file. We can name the file
in any way we want. As a convention, we will name a file that contains
static content with the suffix .html. For example, the page you are reading
right now is named severside.html. The other commonly used file suffix
for static webpages is .htm. A web server can be configured to recognize
any file with .html or .htm as static webpages and will just read the
content of the file and send it to a web browser when requested. 8.1.4.2 Directory configuration While we can associate different file suffixes with both static and dynamic webpages, it is also possible to configure a web server to force any file under a certain directory to be executed. 8.2 CGI/Perl8.2.1 System requirementCGI is simply a standard through which a web server communicates with external programs or scripts written in different programming languages such as Perl, C and Java, etc.. To enable server-side processing using CGI, three minimal conditions must be met:
A system administrator is able to perform the first two tasks, whereas a user is responsible for meeting the third requirement. 8.2.2 Get started with CGI/Perl programmingIf you are interested in the details of CGI/Perl programming, please take a look at this tutorial that provides more technical details than the two examples to be discussed in the following section. 8.2.3 ExamplesThe two sample pages to be discussed below reside on a Unix machine running the Apache web server. The server is configured to recognize the .cgi suffix as a CGI script. Example 1This is a dummy example. You could achieve the same result with a static webpage. First, please take a look at the sample page. You'll see the text string "Hello, World! This is me." in the browser window. The page is generated by a Perl script shown below: #!/usr/local/bin/perl print "Content-type: text/html\n\n"; print <<QUOTEMARKER; <HTML> <HEAD> <TITLE> This is a test page </TITLE> </HEAD> <BODY> <H1>Hello, world! This is me.</H1> </BODY> </HTML> QUOTEMARKER To enable the script to run on a Unix machine, the following steps are taken: 1) Code the script (see above) and save the file as, for example, test.cgi Note that a CGI script is not necessarily to be named with the .cgi suffix. Any suffix (or no suffix) will do as long as the web server is configured properly. Web server configuration is a job for system administrators. 2) Save the file under a directory on the web server where execution is allowed. In this case, it's under the /cgi-bin/ directory. Note: A CGI script is not necessarily to be saved in a directory named as cgi-bin. Any directory will do as long as the directory is configured to allow scripts to be run under it. Again, this is the job for system administrators. On a Unix system where the Apache web server is running, it is a convention to only allow user-written scripts to be run under the ~/public_html/cgi-bin directory of a user's home directory. Again, it is of pure convention but not necessity. 3) Make the script executable by issuing the following command on a Unix system: chmod a+x test.cgi 4) To run the script, type in the proper URL in a web browser. In the case of the sample page, it is: http://lifesci.utexas.edu/cgi-bin/test.cgi To learn more about how to code in Perl, you may want to take a look at www.webmonkey.com or www.perl.org. Example 2Take a look at this sample page first. What you will see is that after you type in a text string in lowercase (in ASCII format) and click on the Submit button, your input string will be converted into uppercase. If you type in a uppercase string, nothing will be changed. It takes two webpages to get this script working: A webpage with web form element (text field) in it to allow data entry. A script is required to process the data. The following is the html source code of the sample webform page:
Pay attention to the line <input type="text" name="" size="20"> where a text field name 'mystring' is given. This is the form variable which will be called up in the following Perl script that processes the web form: #!/usr/local/bin/perl # process the user's input submitted thru the web: read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # Un-Webify plus signs and %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } # We convert the input string into uppercase by using the 'uc' function: $mynewstring = $FORM{'mystring'}; $mynewstring = uc $mynewstring; # we generate the output page print "Content-type: text/html\n\n"; print <<MARKER; <HTML> <HEAD> <TITLE>Here is the uppercase string </TITLE> </HEAD> <BODY> <H1>Here is the upper case string</H1> <p><b><blockquote>$mynewstring</blockquote></b> </BODY> </HTML> MARKER 8.3 Authoring toolsVery often we may want students to submit their online homework to an instructor through email or we want to create a web forum to allow group discussions among students. These are examples where server-side scripting is necessary. Just like we can use authoring tools to create dynamic webpages using client-side processing, there are a few authoring tools which allow us to incorporate these functions without any serious programming. Microsoft FrontPage is one of these authoring tools. 8.3.1 Microsoft FrontPageThe following are four sample webpages that require server-side processing. All of them were created with FrontPage. It takes only a few mouse clicks to get each page working.
In order to take advantage of the many functions available in FrontPage, a web server has to have the so-called 'FrontPage Server Extensions' installed and configured properly. Again, you need to seek the help of system administrators. 8.3.2 WebCTWebCT is an online instruction environment rather than a pure authoring tool. However, it provides functionality that allows dynamic webpages to be created using server-side technology. For more information, please visit its web site at www.webct.com. 8.4 Other optionsWhile CGI is sufficient for processing tasks that do not require intensive server resources, there are other options available for heavy-duty tasks. An additional advantage is programming convenience. You may have seen webpages with .asp or .php3 (or php) suffixes. Those pages are created through a method that applies embedded server-side scripts within an HTML page and are dynamically interpreted when the pages are served. Webpages with the .php3 or simply .php are coded using the PHP scripting language. PHP is a server-side scripting language that can glue dynamic content and static content together. Click here to see a sample display. The following is the PHP script that performs the function, i.e., converting input string into uppercase: <H1>Here is the uppercase string</H1> <blockquote><b> <? $mynewstring = strtoupper($mystring); echo $mynewstring?> </b></blockquote> The magic is done with the blue embedded line. It is even simpler than the Perl script above that performs the same function. The most important advantage of PHP is its many functions specially designed for generating dynamic webpages. These include database interface functions and other web-related network functions. Webpages with the .asp suffix are often written in VisualBasic. They work in a similar fashion as PHP. 8.5 Dynamic contents and templatesWhen a website becomes big, we need to use templates to generate dynamic webpages. Using template will reduce our workload and time spent on page modifications. For example, if we want to deliver a complete course online, we may need hundreds of webpages. It is possible that we create each page on an individual basis. However, it is more efficient that we use template to reduce our labor in page creation and modification. 8.5.1 What is a templateEvery webpage has a structure. For example, the page you are reading right now can be divided into three components: the header (that contains the title of this tutorial), the body (the content of this section) and the footer (which contains the Table of Contents of this tutorial). The body of this page can be further divided into title, subsection title, etc.. From the perspectives of webpage design, a template is simply a file that describes the structure of a webpage. For example, we can create a template file that describes the structure of this page:
Remember that a webpage can be given different structural description when necessary. 8.5.2 How to use a templateServer-side processing is necessary if we want to use templates to generate dynamic webpages. (Placeholder: Sample pages here) Example 1: Using server-side include technique Example 2: Using template with Microsoft FrontPage Example 3: Using template with PHP scripting 8.6 SecuritySecurity is always an issue for server-side processing. Security concerns are of the following areas:
Usually security is a concern for system administrators. As a language instructional professional, we should be made aware of the dangers on the Internet.
Next topic ==> |