|
|
|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Feature Creating a Database Using HTML Forms
Getting Started with Smans HTML form
By: Sergey Savushkin
Aug. 24, 2008 05:46 AM
This article is aimed at beginner and intermediate Web developers looking to make the leap into database support of their Web site. The article suggests a new declarative language based on HTML-forms, which is used for development of a database interface. HTML forms can manage not only the creation of a database, but also the execution of requests to the database. Creating a database by means of HTML formsAs we know, HTML form is a block of HTML page allowing to enter data from the Web browser on a local or remote Web server. The form block is marked with tags <form ...> and </form>. Fields inside of HTML forms may have different types, for example a list or a text field. The Listing 1 specifies code of a form example and Fig. 1 shows view of that form in Web browser.
Listing 1. HTML form code<form method="post" action="..."> Fig 1. HTML form viewThe HTML form is used usually to enter only one record in the table of a database. Each name attribute defines the name of the respective form field and input record. Simultaneously, for Smans server, the same name is considered as a name of a column in the corresponding table. The additional information, usually invisible to the user, is transmitted to the Smans server due to managing fields of HTML form. Most important managing field is $Table. The action attribute in the form descriptor sets up /servlet/record servlet name, receiving and processing the HTML form with operating fields. For example: <form action="../servlet/record" method="post"> The $Table field designates a table formed in the database. Let's consider the example of the recording HTML form making Order table in Listing 2 and Fig. 2. Listing 2. Code of recording HTML form<form action="../servlet/record" method="post"> Fig 2. View of recording HTML formEach input record has to have primary key field(s). In this case, Order table will have Order key column. Because Order field is missing in HTML form, a value of Order primary key field is generated automatically by Smans server as number of table record (row) in increasing order. If Order table in a database still is not present, it will be created. In case the table already exists with other columns, the new columns will be added. As a result, the table will have at least the following list of columns: Order For the event of successful record, you can easily set up the message or the subsequent actions by means of the $Forward field. For example: <input type="hidden" name="$Forward" value="Confirm.html" /> All the managing fields sets up managing HTTP request parameters. In addition to the mentioned above, there are $PKey parameters. They are useful if the table has several primary key columns, when it is necessary to prevent the reoccurrence of several column values. Each $PKey parameter points to the name of conventional parameter (field) in the HTML form, for example: <form action="../servlet/record" method="post"> There are corresponding examples in the distribution kit of the Smans server. They are in a hforms directory. The simplest examples are realized in <wapp>/hforms/Order.html and <wapp>/hforms/Product.html HTML pages. Check up an example. The http://<domain>[/<application>]/hforms/Order.html is address of Order.html page from a Web browser. When you enter the record, the Smans server gives you a message, by default: Data recorded. Thanks! Dialogue scenario through HTML formsIt is a normal situation when the page image depends on certain input parameters processed through PHP, JSP or ASP. Such parameters can be type of a Web browser, human language, personal data of the user or his interests, etc. The part of parameters is defined from the initial user request. The part of parameters undertakes from cookies, kept in a client computer in the previous sessions. Other part is established during dialogue. Dependencies of some parameters on the other are data that are easier to store separately in the database than alternately inside of operating scripts or programs having a lot of other directives. Importance of such approach is not only in plain presentation. The database system allows to find easily the necessary groups of parameters and dependences by inquiry. In addition, the developer has an opportunity easily to change values of parameters and their dependences, and therefore a particular scenario dialogue, not touching the relevant scripts or programs. Let's consider a common situation involving a different resource provision to different users. The necessary dependences and a corresponding database can be generated during registration of the user. In my example, Listing 3, such resources are news. The $Table managing field specifies Username key field, beside the table. The user selects interesting news, which are in the ExpectedNews field. Dependence Username-ExpectedNews is kept in a database of schema Username(Password, ExpectedNews). Listing 3. Recording HTML form. User registration.<form name="form1" action="../servlet/record" method="post"> If registration is successful, next control is passed to the hforms/Login.html page by $Forward. This login page has managing recalling HTML form. Look at Listing 4. The value ../servlet/recall is Smans' servlet, processing the form fields. The $Param fields specify the parameters transferred to an Internet resource pointed in $Forward. The values of $Param parameters are selected from a database in accordance with dependencies: {input data} -> $Table -> {$Param, ...}. Listing 4. Calling HTML form. User login.<form name="form1" action="../servlet/recall" method="post"> Logic of calling HTML form for each $Param can be seen in SQL as: SELECT $Param FROM $Table WHERE {input data}
Then all the found parameters will be transferred by $Forward address (URL), in my case to hforms/News.jsp, like hforms/News.jsp?Username=...&ExpectedNews=...&ExpectedNews=... . Beside JSP, any other resource could as value of $Forward, such as PHP server page, servlet, Perl script and so on. Possible fragment of JSP is figured in Listing 5. Listing 5. JSP fragment. Server response.<% The example described in this section, is realized with Register.html, Login.html, and News.jsp files within hforms directory of free and open source Smans server. ConclusionMy task was to show how you can quickly and easily develop a dynamic Web site without knowing SQL and without being a DBMS expert. Considered tools are quite enough to design the big branchy dialogue, when each previous calling HTML form forwards parameters to JSP/PHP/ASP page, which in its turn may contains a next calling HTML form, and so on. It is important to remember that hidden fields inside of HTML forms cannot be really hidden from the user. Nevertheless, Smans data are quite protected. Tables are created and used only from those HTML pages which are located inside of Web application working with Smans.
LATEST OPEN WEB DEVELOPER STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING OPEN WEB DEVELOPER NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||