blob: 2b4a4d85da614f9ef25e0ed0efc210e7cebfc78a [file] [log] [blame]
## nca-115-2
## $Id$
startHtmlPage
startHtmlBody
putHeader 2
web::putx {
<table border="0" cellpadding="0" cellspacing="0" width="770">
{headRow 3 "Websh example code"}
<tr valign="top">
{navbar_docu 4}
<td width="610">
This section presents a selection of sample Websh applications.
All applications are described in detail on this page and come with
richly documented source code. For simplicity, they are all-in-one
files. Plus, they are on-line and ready to use. For each application
discussed below, you will find the corresponding links which present
the styled code and the plain text version for download.
<br>
<a name="toc"><a href="#toc"><h1>1 Table of Contents</h1></a>
<br>1&nbsp;<a href="#toc"><b>Table of Contents</b></a><br>
<br>2&nbsp;<a href="#13"><b>hello world</b></a><br>
<br>3&nbsp;<a href="#26"><b>command dispatching</b></a><br>
<br>4&nbsp;<a href="#41"><b>image</b></a><br>
<br>5&nbsp;<a href="#51"><b>confirmation form</b></a><br>
<br>6&nbsp;<a href="#62"><b>checkbox form</b></a><br>
<br>7&nbsp;<a href="#72"><b>upload file</b></a><br>
<br>8&nbsp;<a href="#83"><b>sessions</b></a><br>
<br>9&nbsp;<a href="#103"><b>cookie</b></a><br>
<br>10&nbsp;<a href="#111"><b>memory game</b></a><br>
<br><br>
<a name="13"><a href="#toc"><h1>2 hello world</h1></a>
In order to get a first impression, let's look at a simple example.
All it does is printing &quot;Hello, world&quot;. It consists of one websh
command, <tt>web::put</tt>. Basically, <tt>web::put</tt> works like Tcl's
<tt>puts</tt> command. In addition to printing the text, it does send HTTP
headers when invoked for the first time.<br>
<a href="examples/helloworld.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a>
|
<a href="examples/helloworld_ws3.txt">download</a>
<a name="26"><a href="#toc"><h1>3 command dispatching</h1></a>
Now, let's look at one of the main features of websh: the command
dispatching. This allows you to produce all your HTML page from one
and the same source code. This example produces two HTML pages, the
&quot;greetings&quot; page and a &quot;price list&quot; page (even though, for simplicity,
the price list is empty). It uses <tt>web::dispatch</tt> to switch into one
of these pages, and <tt>web::cmdurl</tt> to produce URLs pointing to each
of these pages.<br>
<a href="examples/dispatch_1.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a>
|
<a href="examples/dispatch_1_ws3.txt">download</a>
<a name="41"><a href="#toc"><h1>4 image</h1></a>
This example demonstrates how to return an image instead of the
normal &quot;text/html&quot; mime type.<br>
<a href="examples/image.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a>
|
<a href="examples/image_ws3.txt">download</a>
<a name="51"><a href="#toc"><h1>5 confirmation form</h1></a>
This example demonstrates a confirmation form. The user is promted a form to
enter, say, order information. The information is validated and, if sucessfull,
an e-mail is sent back to the user.<br>
<a href="examples/emailform.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a>
|
<a href="examples/emailform_ws3.txt">download</a>
<a name="62"><a href="#toc"><h1>6 checkbox form</h1></a>
This example demonstrates how to handle checkboxes. The user is promted a form to
select checkboxes, to display date informations. <br>
<a href="examples/checkbox.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a>
|
<a href="examples/checkbox_ws3.txt">download</a>
<a name="72"><a href="#toc"><h1>7 upload file</h1></a>
This example demonstrates a upload form. The user is promted a form to
enter, local file. The file is validated and, if sucessfull,
the server shows in a confirmation the uploaded data back to the user.<br>
<a href="examples/upload.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a>
|
<a href="examples/upload_ws3.txt">download</a>
<a name="83"><a href="#toc"><h1>8 sessions</h1></a>
This example demonstrates file-based server-side sessions. Suppose a
customer visits this application for the first time. The application
creates a new session file using <tt>state::init</tt> and stores the
session id in the query string. From now on, <tt>state::init</tt> will know
the session id and re-load the session context - the application
&quot;remembers&quot; the customers settings across multiple HTTP requests using
a session id embedded in all generated URLs. For simplicity, we do not
add shopping items to a shopping cart in this example, but just save
the data of two forms which are presented to the customer. This
involves a little bit of form handling, and this example also
demonstrates error handling in case of unexpected input. But let's
look at the example.<br>
<a href="examples/session_form.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a>
|
<a href="examples/session_form_ws3.txt">download</a>
<a name="103"><a href="#toc"><h1>9 cookie</h1></a>
This example demonstrates how to use cookies. It is very similar to the
session example above.<br>
<a href="examples/cookie.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a> |
<a href="examples/cookie_ws3.txt">download</a>
<a name="111"><a href="#toc"><h1>10 memory game</h1></a>
This is a more complex example. You know the game &quot;memory&quot;, right ?
The goal is to find matching pictures with the least possible number
of tries. The application keeps track of the number of your tries for
a given game and level and compares it with the &quot;hall of fame&quot;. This
version does not use sessions except for the &quot;hall of fame&quot;, and
stores all information in the URL, which makes the output a little bit
bulky. Have fun.<br>
<a href="examples/memory.html" onclick="javascript:window.open('','source','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');" target="source")>source</a>
|
<a href="examples/memory_ws3.txt">download</a>
</td>
</tr>
</table>
}
finishPage 0