1.5 Give me an example of a Webware servlet!

This example uses an HTML form to ask the user's name, then invokes itself again to display a personalized friendly greeting.

<HTML><HEAD><TITLE>My Template-Servlet</TITLE></HEAD><BODY>   
#set $name = $request.field('name', None)
#if $name  
Hello $name
#else
<FORM ACTION="" METHOD="GET">
Name: <INPUT TYPE="text" NAME="name"><BR>
<INPUT TYPE="submit">
</FORM>
#end if
</BODY></HTML>

To try it out for yourself on a Webware system:

  1. copy the template definition to a file test.tmpl in your Webware servlet directory.
  2. Run ``cheetah compile test.tmpl''. This produces test.py (a .py template module) in the same directory.
  3. In your web browser, go to test.py, using whatever site and directory is appropriate. Depending on your Webware configuration, you may also be able to go to test.

At the first request, field `name' will be blank (false) so the ``#else'' portion will execute and present a form. You type your name and press submit. The form invokes the same page. Now `name' is true so the ``#if'' portion executes, which displays the greeting. The ``#set'' directive creates a local variable that lasts while the template is being filled.