More Dynamic Web Pages With IntranetWare
Articles and Tips:
01 Jun 1997
Editor's Note: This article is the second part of a two-part seriesthat explains how to use IntranetWare to publish dynamic HyperText MarkupLanguage (HTML) documents. The first article, which appeared in the Mayissue ofNetWare Connection,explained how to use Server Side Include(SSI) commands and NetBasic scripts to create dynamic elements in HTML documents.(See "Dynamic Web PagesWith IntranetWare,"pp. 6[shy ]20.)
After you master HTML, you can use the Novell Web Server 3.1 componentof IntranetWare to create dynamic documents. When a user requests a dynamicdocument, Novell Web Server 3.1 or the user's World-Wide Web (WWW) browsergenerates the entire document or a portion of the document, depending onhow the document is created. Using dynamic documents, you can customizeinformation for Internet or intranet users.
Perl was developed as a UNIX management tool to read text files and printreports based on these files. Perl does more than just process text, however:Perl is a sophisticated, general-purpose programming language that offersmath functions, file system functions, and objects. Perl scripts for IntranetWareare programs written using Perl 5, the latest version of the Perl scriptinglanguage.
Like NetBasic scripts, Perl scripts can generate entire HTML documents.However, Perl scripts can also send files other than HTML files, such asimages called from an HTML>IMG>tag.
Perl, like NetBasic, is an interpreted language. As a result, the IntranetWareserver uses a Perl interpreter, which runs as a NetWare Loadable Module(NLM), to process Perl scripts. When you install the Novell Web Server 3.1component of IntranetWare, the PERL5 NLM is installed in the SYS:INW_WEB\SHARED\LCGI\PERL5directory.
Because the Perl interpreter for IntranetWare runs as a Common GatewayInterface (CGI) application, you cannot run Perl scripts from the IntranetWareserver console. Novell Web Server 3.1 loads the Perl interpreter only whena user requests a Perl script. (For information about CGI applications,see the "CGI Solutions for Novell Web Server 3.0" section in DynamicWeb Pages With IntranetWare," NetWare Connection, May 1997,p. 20.)
Unlike NetBasic, which can interact with the IntranetWare operating system,the file system, and the Novell Directory Services (NDS) database, Perlcan access only the IntranetWare file system. Because Perl's strength liesin its ability to manipulate data, Perl is often more powerful than NetBasicfor working with files.
As a security measure, Novell Web Server 3.1 can access only Perl scriptsthat are stored in the SYS:\INW_WEB\SHARED\DOCS\LCGI\PERL5 directory. Ifyou want to use Perl scripts, you must ensure that the scripts are storedin this directory.
You can assign Perl scripts any valid filename. By convention, however,you should use the .PL extension for Perl scripts, although this extensionis not required.
Understanding the Perl Script Environment
Before you begin writing Perl scripts, you should understand how a user'sbrowser requests a Perl script and how Novell Web Server 3.1 calls and processesthat Perl script. Novell Web Server 3.1 calls and processes a Perl scriptin much the same way that it calls and processes a NetBasic script, withone major exception: Novell Web Server 3.1 sends a request for a NetBasicscript to the CGI2NMX NLM, which then converts the request into NetWareModule eXtension (NMX) commands that the NetBasic interpreter can understand.In contrast, the PERL5 NLM is a CGI application, which means that NovellWeb Server 3.1 can communicate directly with the PERL5 NLM; no intermediateNLM is required.
Like NetBasic scripts, Perl scripts run only when a user requests them.To request a Perl script, a user types the request directly into a browseror clicks on a hypertext link that requests the Perl script. In either case,the user's browser requests a uniform resource locator (URL) similar tothe following:
In the server resources map (SRM.CFG) for Novell Web Server 3.1, each virtual WWW server is configured to pass any requests that include /perl/after the site name to the PERL5 NLM. (For more information about virtualWWW servers and configuring Novell Web Server 3.1, see "Electronic Publishing With IntranetWare: Just Another Branch on Your NDS Tree," NetWare Connection, Mar. 1997, pp. 6[shy ]18. If the PERL5 NLM is not loaded, Novell Web Server 3.1 loads this NLM and then passes the Perlscript request to the NLM. The Perl interpreter then executes the Perl script,sending the resulting HTML document back to Novell Web Server 3.1, which,in turn, passes this document to the user who requested it. (See Figure 1.)
Figure 1: When a user's browser requests a Perl script, Novell Web Server 3.1 passes this request to the PERL5 NLM, which runs the requested script and returns the result.
Creating Dynamic Documents With Perl Scripts
Perl scripts are ASCII text files that you can create using any texteditor or word-processing application. After you create a Perl script, youmust place it in the SYS:\INW_WEB\SHARED\DOCS\LCGI\PERL5 directory.
The code below shows a small Perl script and theHTML document this script generates. Line 1 tells the Perl interpreter touse the Perl script library for Novell Web Server 3.1. You can use the routinesincluded in this library to manipulate CGI input and to format output.
PERL SCRIPT 1 require("cgilib.pl"); 2 print &PrintHeader; 3 print "<HTML>\n"; 4 print "<HEAD><TITLE>A Perl Script</TITLE></HEAD>\n"; 5 print "<BODY>\n"; 6 print "Hello, Web clients!\n"; 7 print "</BODY></HTML>\n"; RESULTING HTML DOCUMENT <HTML> <HEAD><TITLE>A Perl Script</TITLE></HEAD> <BODY> Hello, Web clients! </BODY></HTML> Note: Line numbers displayed next to the Perl script are not part of the script itself.
Line 2 calls the PrintHeader subroutine from the Perl script library.The PrintHeader subroutine sends a message to Novell Web Server 3.1, indicatingthat the Perl script will be sending an HTML document.
Lines 3 through 7 demonstrate the Perl print command. When writing aPerl script to create a dynamic document, you use this command to returndata to Novell Web Server 3.1, which, in turn, returns the data to the user'sbrowser. You must also include all of the HTML tags you want in your dynamicdocument in a Perl print command.
Creating Links to Perl Scripts
After you have written a Perl script, you must create a way for usersto request this script and view the resulting HTML document. The easiestway to provide access to a Perl script is to create a hypertext link inan HTML document such as your WWW home page. To create such a link in astatic document, you would add a line similar to the following:
<a href="/perl/script.pl">Launch Perl Script</a>
If a user viewed a static document containing this line, he or she wouldsee"Launch Perl Script"as a hypertext link. When the user clickedon the link, Novell Web Server 3.1 would instruct the Perl interpreter torun the Perl script. The user would then see the dynamic document generatedby script.pl.
Of course, there are other ways to create links to a Perl script: Youcan write a NetBasic script or a Perl script that creates an HTML document,which contains a hypertext link to a Perl script. This Perl script, in turn,generates a dynamic document. For example, suppose that you included thefollowing HTML line in a Perl print command:
<a href="/perl/script.pl">Launch Perl Script</a>
In this way, you would create a link to script.pl within another Perlscript.
As a result, the document.write() method is not limited to printing data.This method can also print HTML tags that a user's browser parses, or interprets.The browser then displays the results for the user.
<HTML> <HEAD><TITLE> Example of Event Handlers</TITLE> </HEAD> <BODY> <H3>Example of onMouseOver and onMouseOut Event Handlers</H3> <A HREF="" onMouseOver="window.status='A message: Hello!' ; return true;" onMouseOut="window.status=' ' ; return true;"> Point to this text</A> and look at the status bar. </BODY> </HTML>
Java is an object-oriented programming language designed by Sun Microsystems.Originally, Java was designed as a platform-independent programming languagefor controlling consumer electronics. Because Java is platform independentand portable, it is the ideal language for developing applications for theInternet.
With Java, you can write small Java applications--calledapplets--thata Java-enabled browser can download from an Internet or intranet serverand run safely on any platform.
The Java language syntax is similar to the syntax of the C programminglanguage, so C and C++ programmers can easily learn to use Java. Despitehaving similar syntax, however, the C programming language and Java arevery different: C, C++, and other compiled programming languages must compilea different application for each platform. Compiled Java applications andapplets, on the other hand, run on any implementation of the Java VirtualMachine. As a result, Java applets are completely portable and platformindependent.
Running Java Applets From an HTML Document
You place Java applets in HTML documents in much the same way that youinclude images. To include an image in an HTML document, you use the HTML>IMG>tag and specify in the SRC parameter the name of the file thata user's browser should download and display. To include a Java applet inan HTML document, you use the HTML>APPLET>tag and specify in theCODE parameter the name of the file that a user's browser should downloadand pass to the browser's Java Virtual Machine. The code below shows an HTML document that calls a Java applet.
<HTML><HEAD> <TITLE>Load an Applet</TITLE> </HEAD> <BODY> Below this text is a Java applet:<BR> <APPLET CODE="Applet.class" WIDTH=150 HEIGHT=100> </APPLET> </BODY></HTML>
The<APPLET>tag is similar to the<IMG>tag in other ways.For example, you have the option of defining the height and width of thearea in which the Java applet will appear. However, the<APPLET>tag differs from the<IMG>tagin one important way: The opening<APPLET>tag requires a closing</APPLET>tag.
If a user's browser does not support Java applets, the browser will displayany text that you place between the opening and closing APPLET tags. Forexample, you could use the following lines to notify a user that his orher browser did not download and run an applet:
If your browser were capable of running applets, you'd see a Java applethere.
What Java Applets Can and Cannot Do
Because Java is a complete programming language, Java applets can doalmost anything. As mentioned earlier, you can use Java applets to drawgraphics or display text. Java also includes a complete toolkit of GUI objects.You can use objects such as menus, text fields, radio buttons, and checkboxes to display information and receive input from the user. Java appletscan also play sounds, open new windows, control the information in thesewindows, and interact with the network to download files. Java can eveninteract with CGI applications running on your server.
However, Java applets do have some limitations. For example, Java appletscannot perform the following functions:
Write Data to Workstation Drives Without Permission. Some browsers, such as Netscape Navigator, never allow Java applets to write data to the hard drive on a user's workstation--even if the Java applet has the necessary rights.
Read Data From Workstation Drives Without Permission. Netscape Navigator never allows Java applets to read data from the hard drive on a user's workstation.
Read From or Write to Arbitrary Memory Addresses. Java applets cannot access memory outside of the Java Virtual Machine.
Introduce Computer Viruses.
The Java Virtual Machine enforces these limitations, which ensure thatJava applets provide a secure method of delivering applications over theInternet.
With IntranetWare, you can create dynamic documents that provide realvalue to your users. Through dynamic documents, users can access your company'sdatabases, read up-to-date company news, and access just about any informationthat you can store on an IntranetWare server.
Terry L. Jeffress works for Niche Associates, which is based in SaltLake City.
NetWare Connection, June 1997, pp.16-25
CGI Programming on the World Wide Web by Shishir Gundavaram (ISBN 1-56592-168-2). This book offers a comprehensive explanation of the Common Gateway Interface (CGI). Nearly all of the examples in the book are Perl scripts, so you can use most of these examples directly on your IntranetWare server. However, CGI Programming on the World Wide Web assumes that you are using CGI programs on a UNIX platform, so some of the examples will not work with IntranetWare.
Gundavaram includes three chapters on processing online forms: He begins with processing simple forms, moves to more advanced forms, and then ends with passing information between multiple forms. Gundavaram also describes graphics, image maps, cookies, gateways to database applications, and Internet communications. For more information about this book, visit O'Reilly and Associates's World-Wide Web (WWW) site (http://www.ora.com). You can also call 1-800-998-9938 or 1-707-829-0515.
Programming Perl, 2nd edition, by Larry Wall, Tom Christiansen, and Randal L. Schwartz (ISBN 1-56592-149-6). Programming Perl is the authoritative work on using the Perl scripting language, which you would expect since Larry Wall, the inventor of Perl, is one of the authors. This second edition covers every aspect of using Perl 5. For example, chapter 2, "The Gory Details," clearly describes the syntax and semantics of Perl. Other chapters describe with equal clarity how to use functions, how to create data structures, and how to use Perl's object-oriented features. To quote a cliche: If you only buy one Perl book, this book should be the one. For more information about Programming Perl, visit O'Reilly and Associates's WWW site (http://www.ora.com). You can also call 1-800-998-9938 or 1-707-829-0515.
The Perl Language Home Page (http://www.perl.com/perl). The Perl Language Home Page is maintained by Tom Christiansen, one of the authors of Programming Perl. On this WWW home page, you can find announcements about the latest revisions of Perl and links to online resources for learning about Perl.
The Comprehensive Perl Archive Network (http://www.perl.org/CPAN/CPAN.html). The Comprehensive Perl Archive Network (CPAN) contains hundreds of public-domain Perl utilities, Perl documentation, answers to Frequently Asked Questions (FAQs), and source code for various implementations of Perl. More than 60 WWW sites throughout the world mirror CPAN, so you should be able to find a site near you. For a list of sites, download the SITES.HTML file from any CPAN archive.
Instant Java by John A. Pew (ISBN 0-13-565821-7). Instant Java is a Java resource for nonprogrammers. If you want to create dynamic HTML elements such as animated text and graphics, slide shows, and audio clips, you will find everything you need in this book, including complete Java programs on a CD-ROM. You will also find detailed, step-by-step instructions on how to customize the Java applets on the CD-ROM to fit your specific needs. For more information about this book, visit Prentice Hall's WWW site (http://www.prenhall.com/~java_sun). You can also call 1-800-811-0912 or 1-515-284-6751.
Java: The Complete Reference by Patrick Naughton and Herbert Schildt (ISBN 0-07-882231-9). If you want to learn how to use Java, this book is an excellent resource. Beginning programmers can quickly learn the concepts of object-oriented programming and find out how to write, compile, and run Java applications.
Intermediate and advanced programmers can start with the description of the Java language and follow the numerous sample programs to learn how to use more advanced features of Java, such as the Abstract Windows Toolkit and multithreading. For more information about this book, visit Osborne/ McGraw-Hill's WWW site (http://www.osborne.com). You can also call 1-800-262-4729 or 1-510-549-6600.
The Java Class Libraries: An Annotated Reference by Patrick Chan and Rosanna Lee (ISBN 0-201-63458-9). After you start programming in Java, you will need a reference so that you can efficiently use the Java class libraries. The Java Class Libraries: An Annotated Reference includes detailed descriptions of all of the Java class libraries and hundreds of sample programs showing you how to implement various features of the objects and methods found in these class libraries. For more information about this book, visit Addison-Wesley Corporate and Professional Publishing Group's WWW (http://www.awl.com/cp) or call 1-800-822-6339. Outside the U.S. and Canada, call 1-617-944-3700, ext. 5190.
* Originally published in Novell Connection Magazine
The origin of this information may be internal or external to Novell. While Novell makes all reasonable efforts to verify this information, Novell does not make explicit or implied claims to its validity.