Skip to main content

Server clinic: Lightweight Web techniques

Two little-known but useful ideas for server-friendly apps

Cameron Laird (claird@phaseit.net), Vice president, Phaseit, Inc.
Cameron is a full-time consultant for Phaseit, Inc., who writes and speaks frequently on Open Source and other technical topics.

Summary:  While there's a flood of material available on Web programming, most of it aims to create "sizzle" to make a particular impression on end-users. Meanwhile, simple but effective techniques for construction of server-friendly Web applications are too-little known. This month's "Server clinic" explains two useful ideas you can apply with any server-side language. Share your thoughts on this article with the author and other readers in the discussion forum by clicking Discuss at the top or bottom of the article.

Date:  23 Jul 2002
Level:  Intermediate
Activity:  1157 views

Was the first "Server clinic" column serious in advertising Expect as the one language you most need? Several readers have written in since its publication with questions about how far to trust the claim.

The short answer: yes. As the April 2002 column explained, Expect comes closer than any other language to being universal for the sorts of needs system programmers are likely to have with their servers.

That does not mean, though, that you should spurn other languages. My realistic expectation is that everyone who reads this column already knows at least one other language, and that's a good thing. Reader Leam Hall explains a couple of reasons why in a note he posted in this column's discussion forum (click Discuss at top or bottom of this page). Most compelling to him as a system administrator is simply that he can't count on Expect to be available the way sh and even awk are. All UNIX hosts reliably supply these utilities; only a fraction provide Expect "out of the box".

June's column centered on the importance of deployment issues. I certainly understand that to meet an immediate need, it's not practical to install Expect on dozens or even hundreds of hosts. Still, there's a "remote control" technical fix that often helps in these situations. See the discussion forum for more on this topic as well.

Another possible relationship between Expect and other languages is cooperation. I'm currently working on a class that helps programmers who prefer to work in Java, Python, and other languages, but still want the full power of Expect, to combine the strengths of two languages. I'll have more to say on the subject later in the year.

Simple bar charts

Another reader asked whether Expect can even do such jobs as displaying a bar chart on a Web page through the common gateway interface (CGI). That's an easy one, as it turns out; ten lines are more than enough. The technique isn't well known, though. Moreover, many people mistakenly believe it's specific to coding in Perl.

I know at least eight architecturally distinct ways to put bar charts on Web pages. The most broadly applicable appears to have been invented first in late 1994, after Netscape's version 0.9 beta release in October made the "dilation trick" possible. Although it's not often used nowadays, I continue to find this best combines compatibility with a range of browsers and light server load. Dave Siegel was a Web designer who publicized this and related "single pixel" techniques, while Robert M. Free did much the same under the trademarked name of "GIF*DOT". Also, Mal Sutherland taught single pixel techniques in his Web Development series.


Figure 1. Simple Expect-coded bar chart
Simple Expect-coded bar-chart

The simplest dilation coding yields a result such as in Figure 1. You can write this in Expect as:


Listing 1. Source code for barchart_expect.cgi

      #!/usr/local/bin/expect
      
      puts "Content-type: text/html\n"
      
      # This is just an image of a single blue pixel.
      set blue http://starbase.neosoft.com/~claird/tmp/blue.gif
      
      puts "<HTML><BODY>
        <table border='0' cellspacing='1'
                    cellpadding='0' bgcolor='#ffffcc'>
            <tr valign=bottom>"

      set heights_to_chart [list 100 10 30 20 70 59 88 11 66 62]
      foreach height $heights_to_chart {
          puts "<td><img src='$blue' width='15' height='$height'></td>"
      }
      
      puts </tr></table></BODY></HTML>
    

It takes only a few lines more to make the bars "clickable," to add legends, to supply the color through a dynamic calculation, and so on. The important point, though, is to realize that essentially any language -- C, sh, Ruby, certainly Expect, Icon, assembler -- can generate these figures. The next time you need a quick bar chart display, use dilation and your favorite language in a server-side Web generation.


Monitors

Another capability Expect shares with other server-side languages is construction of Web "monitors."

Have you ever needed to keep your eye on a remote variable through a Web application? You can visit a weather site, for example, and just hit "reload" or "refresh" every few minutes to ensure that the temperature you see is relatively current.

That approach has awful aesthetics, of course. Every time you refresh, your browser blanks, at least temporarily. Rather than that annoying distraction, you want a Web page that stays fixed, and simply updates a single text element or graphic. For example, the visual effect might be to change from green to red when a server load exceeds a threshhold, or to display the three most recently connected telephone numbers, or most recently analyzed protein sequences in a large production process.

While many developers want such an application, and I've worked out four different solutions, few Web workers seem aware of how to code such monitors for themselves. The most satisfying is one I call the "DOM 2 monitor." It relies on the ability of client-side JavaScript to update the data attribute of a DOM node.

In this and future "Server clinic" installments, I'll often rotate examples through several different languages. Although I first wrote a DOM 2 monitor in Expect, the instance that follows is in Python, simply to emphasize that the ideas are available for many different languages.


Figure 2. Simple Python-coded DOM 2 monitor
Simple Python-coded DOM 2 monitor

You can see a "live" monitor by following the URL that appears in Resources, below. Notice that most of the page in Figure 2 stays fixed, without any "flashing," while the clock value counts off the seconds at the server. This source code generates the page:


Listing 2. Source code for monitor_python.cgi

      #!/usr/local/bin/python
      
      import sys
      import time
      
      print "Content-type: text/html\n"
      
      print """
      <HTML><HEAD>
      <title>Web application 'monitor' demonstration</title>
      </HEAD><BODY>    
      <p id ="x1">Replace me.</p>
      <p>You need IE 5.5, Netscape 6, or Mozilla 0.9.5,
           or greater, to see this effect.
      </body>
      """
      
      print """<script language="JavaScript" type="text/javascript">
          myP=document.getElementById("x1");
          myTextNode=myP.childNodes[0];
          </script>
      """
      
      while 1:
         time.sleep(1)
	 print """
	     <script>
                 myTextNode.data = 'The time at the server is %s.'
             </script>""" % time.ctime(time.time())
         sys.stdout.flush()
    

In this design, the server-side language does only light duty, simply sleeping periodically while sending JavaScript text for the client to interpret. It's the browser that does the hard work, here. Each burst of data requires the browser to update its JavaScript variables and the display they describe.


Conclusion

Yes, Expect solves problems over a broad span. No, you're probably not better off trying to do everything with Expect, especially not if you're already comfortable with other languages. Yes, Expect does CGI, and it specifically can do Web bar charts and monitors in just a few lines. No, Expect isn't the only language for such Web applications; in fact, I've constructed the two examples here so you can adapt them for any language you might know.

Join me in the discussion forum to talk over Web monitors you've built. I've found it exhiliratingly useful to be able to supervise server statuses and display them for others from any location with a Web browser. Variables fit for monitoring include:

  • Motherboard temperature
  • Measured chemical concentrations
  • Intermediate results in massive telecomm circuit tests
  • System load
  • Accounts currently logged in
  • Partial calculations in large simulations

The next few "Server clinic" columns will cover other Web programming techniques specific to systems programming, the concept of a virtual file system, the meaning of "concurrency", and much more.


Resources

About the author

Cameron is a full-time consultant for Phaseit, Inc., who writes and speaks frequently on Open Source and other technical topics.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux
ArticleID=11229
ArticleTitle=Server clinic: Lightweight Web techniques
publish-date=07232002
author1-email=claird@phaseit.net
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers