This site contains older material on Eiffel. For the main Eiffel page, see http://www.eiffel.com.

2. WRITING A FORM-PROCESSING SYSTEM

Processing Web forms with EiffelWeb is straightforward, as EiffelWeb takes care of parsing the form and makes the visitor's input available to you in the form of Eiffel objects, instances of EiffelWeb classes and their descendants.

Writing a basic script

Here are the basic steps to process the data entered by someone (called the "visitor" in the rest of this document) who has filled a form on your Web site:

  • Write an Eiffel class YOUR_FORM describing your form. YOUR_FORM should be a descendant from the EiffelWeb class CGI_INTERFACE, itself an heir to CGI_ENVIRONMENT which provides access to environment variables.
  • Write in YOUR_FORM an effective version of the deferred feature execute inherited from CGI_INTERFACE. This is the body of your form processing, where you can manipulate the data entered by the visitor and, if desired, produce HTML output for display to the visitor.
  • Either define the make creation procedure inherited from CGI_INTERFACE as a creation procedure of YOUR_FORM, or write your own creation procedure if you need more specific creation actions. The default make starts off everything as needed and calls execute; for most uses this is what you need.

A complete example

Let us build a small example. Our system will simply display the results of any HTML form as an indented list of name-value pairs. The name of the field will appear in bold face. You only need one class, the one called YOUR_FORM above - here CGI_DEMO; it can keep the inherited make as a creation procedure. Here is the class:

class
CGI_DEMO
inherit
CGI_INTERFACE
creation
make
feature

execute is
-- Analyse form's fields and output them as name-value pairs.
do
... See next ...
end

end -- class CGI_DEMO

with execute effected as follows:

execute is
-- Analyse form's fields and output them as name-value pairs.
local
i: INTEGER;
vl: LINKED_LIST [STRING]

do
generate_html_header;
put_basic ("You have submitted the following name/value pairs: %N");

put_glossary_start
from
i := fields.lower
until
i > fields.upper
loop
put_glossary_term (fields.item (i));
put_glossary_definition;
put_line_break;
vl := value_list (fields.item (i));

from
vl.start
until
vl.after
loop
put_basic (vl.item);
vl.forth;

if not vl.after then put_line_break end
end;
i := i + 1;
end
put_glossary_end
end

Procedure execute loops over the elements of the array fields, which holds the names of the fields. For each field it loops over the list of values for that field, which may have zero, one or more elements. Procedure generate_html_header, which like the other features used by this version of execute comes from CGI_INTERFACE, prints on the standard output the header of an HTML reply, which is necessary to avoid getting an error from the Web server.

You will have noted that this text does not contain any HTML code. This is because using EiffelWeb does not require knowing HTML; you can produce output using abstract output procedures such as put_line_break, put_basic and generate_html_header which will take care of generating the appropriate HTML output. If you do know HTML and want to generate specific output, you can do so using the class HTML_GENERATOR (see section 6).

Table of contents | Next chapter