Totally Objects Home Page

Flexible Web Servlets for
VisualAge Smalltalk

What's New? Products Totally Objects Downloads Other Downloads Consultancy VAST Training
Educational Discounts See what our clients think Buy News Groups Consultant's Pack Contact Us



Web Connection is a great feature for integrating applications into your Web server. However, if you wish to add, remove or change the functionality of a live system requires you to build a new version in your VAST development image. Not only is this time consuming, it requires a complete stop and restart of the application. This web page shows you how to use TOScript to add Smalltalk scripts to a web server that can be edited by the administrator without the need of a VAST development image or licence.

Build the Part

Create a new Web Connection part called 'WebServlet', say. Place a single Text part into the page. Open its properties and set 'literal' to 'true'. Connect the aboutToGenerateHtml event of the page to the following method:

    | fileName filePath fs script args st titleStream |

    "Assume that the URL is of the format

    fileName := ((self transaction request wsiVars at: 'QUERY_STRING' ifAbsent: ['']) subStrings: $&) first.

    "Strip the file name to remove any path prefix and then map this to a file in the 'webservlets' directory"
    filePath := CfsDirectoryDescriptor startUpDirectoryPath, 'webservlets', CfsDirectoryDescriptor pathSeparatorString, ((fileName subStrings: $/) last subStrings: $\) last.

    "Open the file and read the contents"
    fs := CfsReadFileStream open: filePath.
    fs isCfsError
      ifTrue: [
        (self subpartNamed: 'Page') title: 'Script Error'.
        (self subpartNamed: 'text1') string: 'No Such Script'.
        ^ self
    script := fs contents.
    fs close.

    "Put the arg-val pairs into a dictionary"
    args := Dictionary new.
    self transaction request formDict keysAndValuesDo: [:k :v |
      k = fileName ifFalse: [args at: k put: v first]

    "Create streams to which the script can write the pages body and title"
    st := WriteStream with: ''.
    titleStream := WriteStream with: ''.
    ^ [
      TobSiInterpreter new
        evaluate: script
        for: st "st can be refered to as 'self' in the script"
        arguments: (
          Dictionary new
            at: 'args' put: args;
            at: 'titleStream' put: titleStream;
      (self subpartNamed: 'Page') title: titleStream contents.
      (self subpartNamed: 'text1') string: st contents
    ] when: ExError do: [:sig |
      (self subpartNamed: 'Page') title: 'Script Error'.
      (self subpartNamed: 'text1') string: 'Script Error'.
      sig exitWith: nil

Save the part. To test the part create a subdirectory called 'webservlets' in the same directory as your image. In it create a file called 'test.txt' and edit it in a text editor so that its contents are as follows.
      nextPutAll: 'The date today is <b>';
      nextPutAll: (Date today printString);
      nextPutAll: '</b><br>'; cr.

    args keysAndValuesDo: [:k :v |
        nextPutAll: k abtAsHtml;
        nextPutAll: ' = ';
        nextPutAll: v abtAsHtml;
        nextPutAll: '<br>'

      nextPutAll: 'Date'

Type the following address in to your web browser (making the appropriate adjustments to reflect the way you have your web server configured):
Assuming that you have everything set up correctly, you should see something like this in your web browser.
The date today is 02/02/00
a = 1
b = 2
c = 3

The description above shows you how to make scripts that have access to 3 values; 'self' (a WriteStream to which you write the pages body), 'tableStream' (a WriteStream to which you write the pages title) and 'args' (a Dictionary containing the arguments given in the URL).

This web page shows just the basics. It would be easy to extent this idea further to give the scripts access to application specific objects. You would probably want to improve the exception handling to make it more robust.

Back to the TOScript page

Back to Totally Objects Home Page