Some Example Code:
List of all examples

[Course | Announcements | Materials | Resources]

The examples can be viewed


Examples by Topic

These links are to examples and notes grouped by topic

  1. In-Class
  2. XHTML
  3. CSS
  4. Javascript/JScript
  5. SSI
  6. CGI (with Forms and Cookies)
    1. CGI in general
    2. CGI Form Basics
    3. How forms are processed
    4. Dynamically generated forms
    5. Cookies
  7. Perl 5
    1. Perl `basics'
    2. `Here' documents
    3. Subroutines
    4. Functions
    5. Database Interface
    6. Inter-process Communication
  8. Servlets

Examples by Name Within Topic

These links are to the named examples within topics

  1. XHTML
  2. CSS
  3. Javascript/JScript
  4. SSI
  5. CGI (with Forms and Cookies)
  6. Perl
  7. Servlets



  1. In-Class Examples

    Any examples that we did as a class are in the in-class directory.

    See also the demonstrations in the materials section of the course website.



    1. Template

    2. Elements

    3. Entities

    4. Character Codes

  3. CSS (Cascading Style Sheets)


    1. Simple

    2. Buttons

    3. Nesting

    4. Centre

    5. Image Replacement for Text

    6. Container size

    7. Bad Layout

    8. See also

  4. Javascript/JScript


    1. dual_presentation

    2. dual-redux

    3. input methods

    4. random_tile.js

    5. nested lists

      Using the DOM to dynamically alter a document and using the onclick event. The DOM is also used in the input examples (above). The onclick event is also used in the random_tile example (above).

  5. SSI (Server Side Include)



    Notes about SSI files at Dal FCS:

    • filenames must end with .shtml
    • files must not be in (or beneath) your public_html/cgi-bin directory
    • the server that runs is not the same as the one that runs torch.cs.dal.caSSI may work differently on them
  6. CGI (Common Gateway Interface)


    1. Notes about the Common Gateway Interface (CGI)

      CGI is an interface. The client can pass parameters to the server through HTML forms and back to the client through generated programs. The server-side programs can be written in any language. For instance here are two programs written in the Bourne shell:

      1. Static output
      2. Dynamic output

      Note that the program file must

      • be readable and executable by the server,
      • be in your ~/public_html/cgi-bin/ directory,
      • be referred to (in hrefs) as being in ~you/cgi-bin/ (where you is your username), and
      • have have .cgi as the last four characters of its name.

      For the interface to work, there must be a header before the content. The header can contain cookies and other information that the server passes on to the browser.

      The last two lines of the header must be:

      1. the MIME-type of the content to be presented to the user
      2. empty

      For example:

      Content-type: text/html

      The MIME types are all specified in RFCs. The two most common MIME types used on the WWW are shown in the following table

      Two Common MIME Types
      Type Encodes
      text/html HTML
      text/plain Text

      For more information about MIME types and their use on the WWW see this excerpt from XHTML 1.0 Web Development Sourcebook and this bug report about Internet Explorer.)

      When we program CGI in perl we use the package. It includes many handy features, such as header() which will produce the MIME-type header for us. If we don't tell it what type of header to produce then it makes one for HTML. Remember header() is a subroutine returning a string, we have to use print to make it appear in the output.

      Here are the same two examples that we saw in Bourne shell but with perl:

      1. Static output
      2. Dynamic output
    2. &


        CGI programs are usually not run as suid (set-uid) for security. Do our CGI program's run as suid? Check out the program:

        Notes about CGI programs on torch:

        • files must be readable and executable by the server (user http);
        • filenames must end with .cgi;
        • files must be in (or beneath) your public_html/cgi-bin directory;
        • Only the file's owner has write permission to the file and the directory;
        • There is a checklist for CGI in perl in the system documentation subsection of the materials.
        • For related information about access control lists see the ACL part of the system documentation subsection.
      2. shows the parameters that are available in a CGI program.

    3. CGI Form Basics


      1. controls-example

        The controls-example form demonstrates the most common types of form controls. The controls-example.cgi program shows code that reads the values (on the server) from the form (submitted on the client).

      2. file-upload

        file-upload is a CGI program that generates different output depending on conditions (like the advice programs). It makes a form that:

        • reads in a file,
        • uses's uploadInfo() function to show some of the information that the browser sent with the file, and
        • reads the file (from the server's cache) to check the file length.
        • Note that the length check depends on the assumption that it is a text file.
        • Note also that the program generates the form itself.

        The program is based on one by Lincoln Stein (see source code for details).

      3. simple-get.html and simple-post.html

        The simple-get.html and simple-post.html forms use the same form but different methods. Look at the URL that they send to the server.

      4. hidden-get.html and hidden-post.html

        The hidden-get.html and hidden-post.html are identical to the simple forms above, but these include hidden fields.

    4. How Forms Are Processed

      Here is more detail about how forms are processed.

      1. how-post-is-processed.html

        This form is almost the same as in the simple form examples above, except that it includes a hidden value. The program shows all three of the ways that data can be passed to the CGI program: through command-line arguments, the environment, and standard input.

      2. how-get-is-processed.html

        This form is exactly the same as the post form (immediately above) except that it uses get instead of post. Both forms call the same CGI program so you can see how the parameters are passed.

        get sends its parameters in the URL and the server receives them in the QUERY_STRING environment variable. On the other hand, post sends its parameters through the standard input device of the server process.

      3. three-penny-form.html

        An XHTML file with three forms (an example using method="get", an example using method="post", and an example of a file-upload using method="post"). Each of the forms calls form-processing.cgi.

        sample.txt is a small file that works well with the file upload.

      4. Notes about form methods and encodings
        Differences Between CGI Methods
        GET POST
        1) Thought of as a read request 1) Thought of as a write instruction
        2) Can be cached 2) Should not be cached
        3) Name=value pairs appear in URL
        • name=value follows a ? after the address
        • each name=value separated by & or ;
        3) Names and values are not visible to the user
        4) Names and values received, on server-side, in environment variable QUERY_STRING 4) Names and values received in standard input (using MIME encoding multipart/form-data)

        How CGI Arguments Are Encoded
        alphanumerics not encoded
        blank (space) as +
        other as %h1h2
        (where h1 and h2 are hexadecimal digits)
    5. Dynamically Generated XHTML forms

      Since CGI can be used to generate XHTML we can write programs that generate their own forms. When the form is submitted the CGI program can then generate a new form that will be different depending on what values were input in the previous form. We can use <input type="hidden"> to tell the CGI program which form it is responding to. (The file-upload.cgi example, above, uses a similar technique.)

      1. advice.cgi generates a form that changes depending on what was submitted the previous time it was run. It uses code in to generate most of the HTML. and are other versions of the same CGI program that do not let generate HTML for it. also includes slightly more error checking code. (The original advice program was written by Lincoln Stein.)

        How advice Works

        None of these programs produces valid HTML output. They are meant only to be simple examples without any confusing details.

        Many web browsers recognize the markup <form> to mean that a method="post" form is being created and that the action is to send the results to the same program that generated the form in the first place. This is not valid HTML but it is sufficient for you to understand how dynamically generated forms work.

      2. multi-page.cgi (and loan.cgi)

        multi-page.cgi and loan.cgi are two examples of using hidden fields in forms to save state. Each of these programs generates a form that changes depending on what was submitted the previous time they were run. multi-page.cgi is a basic and somewhat ugly example. loan.cgi is an elegant piece of perl programming (by Lincoln Stein) that happens to use to generate much of the HTML code.

        For another example of using forms to save state see the time1 example in the Cookies section.

    6. Debugging CGI programs

      These examples of how to debug CGI programs rely on Perl's module to demonstrate the differences between online and offline execution. The programs cannot be executed offline from this website.

      description: demonstration of perl's warn() and die()
      Perl source of debugging.cgi
      location: /~jamie/public_html/cgi-bin/4173/cgi:basic/debugging.cgi
      see also: the Perl/CGI programming checklist in the system documentation section
      description: real-world debugging in a simple form, uses diagnostics pragma for verbose error messages, and includes sanity check code
      location: /~jamie/public_html/cgi-bin/4173/form/advice2a.cgi
      Perl source of advice2a.cgi
      description: using cookies
      Perl source of time1-d.cgi
      location: /~jamie/public_html/cgi-bin/4173/cookie/time1-d.cgi
    7. Cookies


      1. CGI form displaying time (time1.cgi)
        • This form saves state by writing out new default values of the radio buttons every time it is run
        • The time1 and time2 examples are based on examples by Lincoln Stein
        • Run time1
        • view the source code of time1
        • For another example of using forms to save state see the mutlti-page example in the CGI forms section.
      2. same form saving state with cookies (time2.cgi)
      3. cookie form (cookie-colour)
      4. See also the documentation about cookies:

      Notes about Cookies:

  7. perl


    1. perl ‘basics’

    2. `here' documents

      `Here' documents allow you to include a literal document in one print statement. The syntax is:
      print <<end-marker
      text to print


      • The end-marker must be immediately following the <<
      • If the end-marker is quoted (with '', "", or ``, etc.) then the print statement acts as though each line in the here portion is quoted that way too. (Use '' to quote literaly, as in the here.cgi example below).
      • The semi-colon (;) that follows the second end-marker almost always needs to be on a separate line from the end-marker
      • If you are having trouble with your own here documents then read the question about them in section 4 of the perl FAQ (archived copy of perldoc perfaq4 on torch).

      Here is an example that uses CGI:

      Another example

      (from Programming Perl page 67):

      If you want your here docs to be indented with the rest of the code, you'll need to remove leading whitespace from each line manually:

             ($quote = <<'QUOTE') =~ s/^\s+//gm;
                 The Road goes ever on and on,
                 down from the door where it began.
    3. subroutines


      Parameters are passed by value, but you can pass references.

      How to pass more than one list to a subroutine:

    4. Functions


      The program demonstrates how to use the stat function to find the modification time of a file. It can run as a CGI program or from the command-line.

    5. Database Interface Examples


    6. Inter-process Communication


      These examples are all perl code that demonstrates aspect of the client-server model and how to implement it using Perl. The example source code listings are in the perl/ipc directory.

  8. Servlets


    Note about servlet examples:
    These servlets were compiled for a different Servlet/JVM combination than is currently installed. The source code is still correct however, and the compilation record can be used as a guide. But the compiled versions might not be executable now.

    For details about how servlets are supposed to configured, etc. see the How Do I Set Up Servlets? FAQ at the CS Student Services help website. (The link was current on 06 February 2007.)

    1. HelloClient


    2. Greeting


    3. Session Tracking


      • run ShowSession on torch
      • view file system directory containing the source code of ShowSession
      • Slightly modified from code by Marty Hall and Larry Brown
      • Notes:
        • The session tracking is handled by the servlet API (either by cookies or URL rewriting).
        • Because we don't know if the client will use cookies (or not) we can't use the same URL each time, so we must call request.encodeURL or request.encodeRedirectURL to generate the correct form of the URL.
        • session.getMaxInactiveInterval() and session.setMaxInactiveInterval() are used to access the number of idle seconds before a session expired (and is closed automatically). Negative values mean that the session never expires.
        • Sessions can be terminated with invalidate().
    4. Servlet Request Headers


    5. Marty Hall's Examples



to Top
digit 3
go to
digit 0
go to
accesskey legend