Edward Parrish © 2003     

5. Server-Side Programming

What We Will Cover


Log Tails

From Last Lab

Quiz Review


5.1: Dynamic Documents

Objectives

At the end of the lesson the student will be able to:

  • Describe why programs are used to generate documents
  • Plain HTML documents are static -- don't change once created
  • Dynamic documents are documents that change during response or later
  • Many different ways to generate dynamic documents
  • In this section we discuss some uses for dynamic content
  • Remaining sections we explain some of the most common methods

5.1.1: Dynamically Generated Web Pages

  • In normal HTTP transactions, server just locates and returns the document
  • With a dynamically generated document, server does a little more work
    • Server might execute a program and return the output of that program
    • Program could pull information out of a database and return it in HTML format
  • Providing information directly from a database saves time updating static pages
  • Can also ensure visitor has latest information
  • For example: providing prices and inventory for an online store

5.1.2: Dynamic HTML

  • Dynamic HTML uses combination of client-side technologies that produce dynamic documents
    • Cascading style sheets (CSS)
    • Client-side scripting (e.g. JavaScript)
    • Animated GIFs

  • Can create simple animations, images, and text that changes as a mouse moves over them
  • These technologies, though interesting, have little to do with a server
    • All processing is performed in visitor's browser
  • Client-side scripting has many problems:
    • Not all browsers support client-side scripting languages equally well
    • Changing HTML to update content is often problematic
    • Changing common visual elements on large sites can easily create errors
  • Can solve some of these problems with server-side dynamic content
    • Avoids many browser incompatibilities by using "standard" HTML
    • Can tailor HTML and client-side script to browser type
    • Can connect to databases to select latest information
    • Can isolate common page elements in one file to ensure changes are applied appropriately

5.1.3: Server-Side Scripting Technologies

  • Server-side scripting is popular way to implement dynamic content
  • Applications using server-side scripting rely heavily on HTML forms
  • Forms allow users to submit data to your server
  • Server-side programs can process and respond to that data
  • Popular server-side scripting technologies include:

Common-Gateway Interface (CGI)

  • One of the oldest standards for running external programs on a web server
  • CGI allows Web server to pass information to another program and receive information back
  • Can use any programming language supported by server
  • Many Web programmers write CGI programs using scripting languages
  • Perl is a popular choice for writing CGI scripts
  • CGI programs usually generate HTML though they can generate any type of data

Active Server Pages (ASP)

  • ASP allows developers to create dynamic Web pages by embedding scripts within documents
  • Web server processes the scripts as the pages are requested
  • ASP is one of the best-known and best-supported web scripting technologies
  • Shipped with most Windows products
  • Multi-platform support via Sun[tm] ONE Active Server Pages product (formerly ChiliSoft)
  • Can use many scripting languages including JScript, VBScript and Perl
  • Uses ActiveX Data Objects (ADO) to connect with databases
  • Very good performance

PHP Hypertext Protocol (PHP)

  • Open source and works on multiple platforms
  • Code is like mixture of Java, C++ and Perl
  • Excellent performance
  • Most popular of all server-side scripting languages
  • Works well on Apache and IIS
  • More PHP information from php.net

Servlets and Java Server Pages (JSP)

  • Servlets analogous to CGI programs: server-side programs executed by Web server
  • Servlets can accept data from forms or queries and output data like CGI
  • Servlets run as part of Web server and can be considered a server extension
  • JSP allows developers to create dynamic Web pages by embedding Java within documents
  • Web server processes the JSP as the pages are requested
  • Sun donated JSP reference implementation to the Apache Group (Tomcat)

ColdFusion

  • One of the easiest scripting languages with good administrative capabilities
  • Reasonable performance
  • Highest cost solution of this list
  • More ColdFusion information from Macromedia

Lab Exercise 5.1

Instructions:

Use the next 5 minutes to complete the following.

  1. Start a text file named exercise5.txt
    Will be adding to this file during the lesson -- save it often.
  2. Prepare the exercise header as described in the HowTo on submitting exercises
  3. Label this exercise: Lab 5.1
  4. Answer the following questions.

Exercises and Questions

  1. What are some other applications of server-side programs (not mentioned above)?
  2. What is the benefit of using JavaScript code in an HTML document?

5.2: CGI and Forms

Objectives

At the end of the lesson the student will be able to:

  • Describe what CGI is and how CGI programs work
  • Describe how HTML forms are used

5.2.1: Common Gateway Interface (CGI)

  • CGI is one of the oldest standards for running external programs on a web server
  • CGI allows web servers to pass information to another program and receive information back
  • Can use any programming language able to run on the server
  • Many Web programmers write CGI programs using scripting languages
    • Perl is a popular choice
  • CGI programs usually generate HTML
    • However, they can generate any type of data

CGI Processing

  • CGI program is requested in the same way that any web page is requested
  • User enters a URL and the server returns a response
  • Server executes the CGI script and returns the output of the program
  • CGI programs should always generate a valid HTTP response and document
  • Web server and client have no way of telling what kind of data CGI program is generating
  • Program must inform them by using the Content-Type header

For Example

    #!/usr/bin/perl
    print "Content-Type: text/html\n\n";
    print "<HTML>\n";
    print "Hello CGI World!\n";
    print "</HTML>\n";
    

5.2.2: Forms

  • CGI can send information from a visitor to a program using forms
    • Allows a client to send a query to the server
  • Server can do something useful with the query and return information
  • Easiest way to submit information to a CGI program is through HTML forms
  • HTML contains several tags for various types of input fields in documents
  • Visitor enters data in the fields and presses submit button

Passing Information to CGI Program

  • HTML form must have a link to the URL of the CGI program
  • Form data is encoded and passed to the CGI program when submitted
  • CGI program can access data through environment variables or through query string
  • Forms can either use GET or a POST method to send information to the server
  • GET methods encode the query as part of the URL
  • POST methods include the query in the entity body of the HTTP request

For Example

    * First Name:

    * Last Name:

    * Mr. Ms. Mrs.

    * In which county do you live?

    Questions or Comments?

    * indicates required field


    Code for the above form:

<form action="lesson05.asp#5.2.2" method="post">
<p>* First Name: <input type="text" name="firstName"></p>
<p>* Last Name: <input type="text" name="lastName"
        value=""></p>
<p>* <input type="radio" name="title" value="Mr">Mr.</input>
<input type="radio" name="title" value="Ms">Ms.</input>
<input type="radio" name="title" value="Mr.">Mrs.</input></p>

<p>* In which county do you live?<br>
<select name="county">
<option value="">-- Select County --</option>
<option value="Monterey">Monterey</option>
<option value="Santa Clara">Santa Clara</option>
<option value="Santa Cruz">Santa Cruz</option>
</select>

<p>Questions or Comments?<br>
<textarea name="userComments"></textarea>
<p>* indicates required field</p>
<p><input type="submit"></p>
</form>

5.2.3: Configuring CGI and Perl

  • CGI directory already enabled for our installation
  • ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
  • Configures Apache to use /usr/local/apache2/cgi-bin/ as CGI directory
  • Entering following location into browser would cause server to invoke hello.pl
  • localhost/cgi-bin/hello.pl
  • To verify CGI setup, complete the following steps:
  1. Create a Perl file in the CGI directory
  2. #!/usr/bin/perl
    print "Content-Type: text/html\n\n";
    print "<HTML>\n";
    print "Hello CGI World!\n";
    print "</HTML>\n";
    
  3. Change access permissions to allow script to execute
  4. chmod 755 /usr/local/apache2/cgi-bin/hello.pl
  5. Start a browser and enter the URL address:
  6. localhost/cgi-bin/hello.pl
  7. Verify correct output appears on browser page
  8. Hello World!
  • Can enable CGI outside of ScriptAlias directory
  • AddHandler cgi-script .cgi .pl
  • Will also need to add "ExecCGI" to the "Options" directive
  • Alias /cis164 "/home/cis164/public_html"
    
    <Directory "/home/cis164/public_html/">
        Options Indexes MultiViews ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    
  • Now any file with .cgi extension executed as a CGI program
    • Do not forget to stop and start the server
  • Copy the above Perl program into the /home/cis164/public_html directory as hello.cgi
  • cd /usr/local/apache2/cgi-bin
    cp hello.pl /home/cis164/public_html/hello.cgi
    chmod 755 /home/cis164/public_html/hello.cgi
  • Enter the URL address into the browser:
  • localhost/~cis164/hello.cgi

Lab Exercise 5.2

Instructions:

Use the next 15 minutes to complete the following.

  1. Label this exercise: Lab 5.2
  2. Answer the following questions

Exercises and Questions

  1. Enable a directory or file extension for CGI programs on your server. What configuration options are used?
  2. Type in the CGI script from the example and verify that it works on your server. What happens when you remove the Content-Type line from the Perl script?
  3. Create an HTML file in your CGI directory. What happens when you try to view it?
  4. Rename a CGI program so that it has a different extension (hello.prg, for instance). Do not forget to change access permissions. What happens when you access it in the CGI directory? Move it to a plain document directory. What happens when you access it now?
  5. What are some common uses of forms?
  6. Write a simple form in HTML. Have it call your CGI script using a POST method. Now modify the HTML to use a GET method. Do you see a difference?

5.3: Server-Side Includes

Objectives

At the end of the lesson the student will be able to:

  • Describe server-parsed HTML

5.3.1: About SSI

  • CGI is useful for processing form data and generating dynamic output
  • However, it requires some programming know-how
  • Generating even a small HTML document with a CGI program requires writing a complete program
    • More complex than writing a standard HTML document
  • Requires significant resources to start a CGI program for each document requested
  • Many documents contain a large amount of static content and small dynamic content
  • In these case, consider using server-parsed HTML
    • Server-side-includes (SSIs)

5.3.2: Adding SSI to Apache

  • SSIs allow you to place special server-side tags into an HTML document
  • SSI tags can execute other programs or display data in your HTML files
  • Server parses the file and processes these tags when document requested
  • Dynamic data is inserted into the HTML document in place of the tags

To add SSI to the Apache Web server you need to do two things:

  1. Add a MIME type for the .shtml extension by adding the following directive to your httpd.conf file:
  2. AddType text/html .shtml

    This tells Apache that files with the .shtml extension are of type text/html. This is the same as normal .html files.

  3. Add an output filter of type INCLUDES to the .shtml type.
  4. AddOutputFilter INCLUDES .shtml
  5. Add Includes to the Options
  6. Alias /cis164 "/home/cis164/public_html"
    
    <Directory "/home/cis164/public_html/">
        Options Indexes MultiViews ExecCGI Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    

More Information


5.3.3: Using SSI

    Save the following in /home/cis164/public_html/ as include.shtml:

    <html>
    My phantasmagorical web page...
    <hr>
    This page was last updated on:
    <!--#echo var="LAST_MODIFIED"-->
    </html>
    
  • Make sure the permissions are set correctly
  • chmod 644 /home/cis164/public_html/include.shtml
  • Will produce the following output
    My phantasmagorical web page...
    
    This page was last updated on: 2/8/2012 1:34:37 AM
  • Common use of SSI is to place "Last Modified" footer in HTML documents
    • See bottom of this page
  • SSI directives not standard on all servers
  • Usually .shtml extension is used for files that contain server tags and need to be parsed
  • Alternatively, can set all HTML files to be parsed

Lab Exercise 5.3

Instructions:

Use the next 10 minutes to complete the following.

  1. Label this exercise: Lab 5.3
  2. Answer the following questions.

Exercises and Questions

  1. What needs to be done to use server-side includes on your server?
  2. What are the most common SSI directives?

5.4: Active Server Pages

Objectives

At the end of the lesson the student will be able to:

  • Describe how Active Server Pages work
  • ASP allows developers to create dynamic Web pages by embedding scripts within documents
  • Web server processes the scripts as the pages are requested
  • ASP is one of the best-known and best-supported web scripting technologies
  • Shipped with most Windows products
  • Multi-platform support via Sun's Chili!Soft product
  • Can run many scripting languages including JScript, VBScript and Perl
  • Uses ActiveX Data Objects (ADO) to connect with databases
  • Very good performance
  • ASP code is enclosed in <% %> tags

5.4.1: Using ASP

  • Could install ASP on your Apache server using Chili!Soft ASP
  • However, does not support our level of Linux
  • Instead, can run this page on IIS
  • <html>
    <body>
    <% for x = 1 to 6 %>
    <H<%= x %>>Header <%= x %></H<%= x %>>
    <% next %>
    </body>
    </html>
    

    Run it here

  • View the source of the page (View->Source)

Lab Exercise 5.4

Instructions:

Use the next 5 minutes to complete the following.

  1. Label this exercise: Lab 5.4
  2. Answer the following questions.

Exercises and Questions

  1. Run the example ASP file and view it in a browser. Does it work?
  2. Compare the code below with the page source

    <html>
    <body>
    <% for x = 1 to 6 %>
    <H<%= x %>>Header <%= x %></H<%= x %>>
    <% next %>
    </body>
    </html>
    
  3. What are the differences?
  4. How do you explain the differences?

5.5: Servlets and Java Server Pages

Objectives

At the end of the lesson the student will be able to:

  • Describe the Benefits of Servlets and JSP
  • Servlets are analogous to CGI programs in that they are server-side programs that are executed by the Web server
  • Servlets can accept data from forms or queries and output data, just like CGI
  • Servlets run as part of the Web server and servlet technology can be considered a server extension
  • JSP allows developers to create dynamic Web pages by embedding Java within documents
  • Web server processes the JSP as the pages are requested
  • Sun donated JSP reference implementation to the Apache Group (Tomcat)

5.5.1: Servlets

  • Servlets are analogous to CGI programs in that they are server-side programs that are executed by the Web server
  • Servlets can accept data from forms or queries and output data, just like CGI
  • Servlets run as part of the Web server
  • Servlet technology can be considered a server extension
  • Web server must include a servlet runner, which is a Java Virtual MAchine (JVM)
  • JVM is an interpreter that allows a computer to run Java programs
  • For Example

  • Simple servlet that prints out the current date and time
  • import java.io.*;
    import java.util.Date;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class DateServlet extends HttpServlet {
    public void doGet(HttpServletRequest req,
      HttpServletResponse res)
      throws ServletException, IOException {
      res.setContentType("text/plain");
      ServletOutputStream out = res.getOutputStream();
      Date today = new Date();
      out.println(today.toString());
      }
    }
    
  • To access this servlet enter a URL like
  • http://localhost/servlet/DateServlet
  • Can see similarities between this servlet and a simple CGI script
  • Content type must be set
  • res.setContentType("text/plain");
  • Get information from a query using the HttpServletRequest object
  • Output data using the HttpServletResponse object
  • Fair amount of programming just to print current date on Web page

5.5.2: JSP

  • Java Server Pages reduce some of the complexity of servlets
  • Allow Java code to be placed directly into HTML files
  • JSP allows the output of Java programs to be included in Web pages
  • JSP is another server-parsed HTML technique similar to ASP
  • For Example

  • Java Server Page that prints out the current date:
  • <html>
    Today's date is <%= new Date() %>
    </html>
    
  • No compilation required since the server takes care of that when necessary

Lab Exercise 5.5

Instructions:

Use the next 10 minutes to complete the following.

  1. Label this exercise: Lab 5.5
  2. Answer the following questions.

Exercises and Questions

  1. What are the benefits of using servlets?
  2. Why are Java Server Pages useful?
  3. What is required to use Servlets and JSP on your web server?
    (Hint: Tomcat documentation has five main steps)
  4. Note: This would be a good student project.


Wrap Up

  • When class is over, please shut down your computer
    Main Menu => Logout => Shut Down
  • Due Next: N/A

  • You may complete unfinished exercises at any time before the next class.
  • Be sure to submit the file to the instructor before the beginning of the next class to receive credit.
  • Instructions on submitting exercises are available from the HowTo's page.

Home | WebCT | Announcements | Schedule | Expectations | Syllabus
| Help | FAQ's | HowTo's | Links

Last Updated: 1/6/2004 12:18:13 PM