CSCI A348/548
Lab Notes Two

Spring 2001 (Second semester 2000-2001)


Getting started with the project.

The project has been explained in class.

Here are the components.

1. The front end.

<html>

<head>
  <title>Foobar Public Library: Add Books</title>
  <style>
<!--
body         { font-family: Arial }
h1           { color: #000080 }
-->
  </style>
</head>

<body link="#FFFF00" vlink="#FFFF00" alink="#FFFF00">
 <table border="0" width="100%" cellpadding="0" cellspacing="0">
  <tr>
   <td width="15%" bgcolor="#000080" valign="top" align="center">
    <b><i>
     <font color="#FFFFFF" size="4">Options</font>
    </i></b>
   <p><b>
     <font color="#FFFFFF">
      <a href="/javaxml/foobar">Main Menu</a>
     </font>
   </p></b>
   <p><b>
    <font color="#FFFFFF">
     <a href="/javaxml/foobar/catalog.html">Catalog</a>
    </font>
   </b></p>
   <p><b>
    <i><font color="#FFFF00">Add Books</font></i>
   </b></p>
   <p><b>
    <font color="#FFFFFF">
     <a href="/javaxml/foobar/logout.html">Log Out</a>
    </font>
   </p></td>
   <td width="*" valign="top" align="center">
    <h1 align="center">The Foobar Public Library</h1>
    <h3 align="center"><i>- Add Books -</i></h3>

    <!-- This will need to point at your CGI directory and script, which
             we look at next -->
    <form method="POST" action="/cgi-bin/addBook.pl">

     <table border="0" cellpadding="5" width="100%">
      <tr>
       <td width="100%" valign="top" align="center" colspan="2">
        Title 
        <input type="text" name="title" size="20">
        <hr width="85%" />
       </td>
      </tr>
      <tr>
       <td width="50%" valign="top" align="right">Author 
        <input type="text" name="author" size="20">
       </td>
       <td width="50%" valign="top" align="left">Subject 
        <select size="1" name="subject">
         <option>Fiction</option>
         <option>Biography</option>
         <option>Science</option>
         <option>Industry</option>
         <option>Computers</option>
        </select></td>
       </tr>
       <tr>
        <td width="50%" valign="top" align="right">Publisher 
         <input type="text" name="publisher" size="20">
        </td>
        <td width="50%" valign="top" align="left">ISBN 
         <input type="text" name="isbn" size="20">
        </td>
       </tr>
       <tr>
        <td width="50%" valign="top" align="right">Price 
         <input type="text" name="price" size="20">
        </td>
        <td width="50%" valign="top" align="left">Pages 
         <input type="text" name="numPages" size="20">
        </td>
       </tr>
       <tr>
        <td width="100%" valign="top" align="center" colspan="2">
         Description 
         <textarea rows="2" name="description" cols="20"></textarea>
        </td>
       </tr>
      </table>
      <p>
       <input type="submit" value="Add this Book" name="addBook"> 
       <input type="reset" value="Rest Form" name="reset">
       <input type="button" value="Cancel" name="cancel">
      </p>
    </form>
   </td>
  </tr>
 </table>
</body>
</html>
The server-side part of adding books.

#!/usr/local/bin/perl

# This should be the directory you wish to write files to 
$baseDir = "/u/dgerman/project/"; 

# This should be the filename to use
$filename = "books.txt"; 

$bookFile = $baseDir . $filename;

# Get the user's input

use CGI;
$query = new CGI;

$title       = $query->param('title'); 
$author      = $query->param('author'); 
$subject     = $query->param('subject'); 
$publisher   = $query->param('publisher'); 
$isbn        = $query->param('isbn'); 
$price       = $query->param('price'); 
$numPages    = $query->param('numPages');
$description = $query->param('description'); 

# Save this book to a file in XML
if (open(FILE, ">>" . $bookFile)) {
    print FILE qq{
      <book subject="$subject">
        <title><![CDATA[$title]]></title>
        <author><![CDATA[$author]]></author>
        <publisher><![CDATA[$publisher]]><publisher>
        <numPages>$numPages<numPages>
        <saleDetails>
          <isbn>$isbn<isbn>
          <price>$prices<price>
        </saleDetails>
        <description>
          <![CDATA[$description]]>
        </description>
      </book>
    }; 
  

    # Give the user a confirmation

    print qq{Content-type: text/html\n\n<html>
      <head><title>Monroe County Public Library: Confirmation</title></head> 
      <body bgcolor=white>
      <h1 align=center>Book Added</h1> 

      <p align=center>
         Thank you. The book submitted has been added to the Library. 
      </p>
      </body>	     
      </html>
    }; 
} else {
    print qq{Content-type: text/html\n\n<html>
      <head><title>Monroe County Public Library: Confirmation</title></head> 
      <body bgcolor=white>
      <h1 align=center>Error in Adding Book</h1> 

      <p align=center>
         We're sorry. The book you have submitted has not been added to the 
         Library. 
      </p>
      </body>	     
      </html>
    }; 
} 
There's one coordinate that you need to set.

The part that makes the file available to another business.

#!/usr/local/bin/perl

# This should be the directory you wish to write files to
$baseDir = "/u/dgerman/project/";

# This should be the filename to use
$filename = "books.txt";

$bookFile = $baseDir . $filename;

# First open the file
open(FILE, $bookFile) || die "Could not open $bookFile.\n";

# Let browser know what is coming
print "Content-type: text/plain\n\n";

# Print out XML header and root element
print "<?xml version=\"1.0\"?>\n";
print "<books>\n";

# Print out books
while (<FILE>) {
  print "$_";
}


# Close root element
print "</books>\n";

close(FILE);
Call it supplyBooks.pl and place it in the same place.


Last updated on Apr 3, 2001, by Adrian German for A348/A548