Java Reference
In-Depth Information
HTML Forms—the Common Gateway Interface
The HTML form is a common mechanism for users to input data to Web applications.
If you have ever visited a Web page in which you enter data using textboxes and buttons,
then you have used an HTML form. In this section, we introduce just a handful of tags
that can be used in an HTML form and then show how submitted data can be processed
by a JSP program. The information you enter into an HTML form is transmitted to the
Web server using a protocol called the Common Gateway Interface ( CGI ) . The server
processes the information using a program such as a Java servlet and then returns the
results back to the user.
An HTML form is created with the <form> tag. The syntax is as follows:
HTML Form
Common
Gateway
Interface
<FORM ACTION=" Path_To_CGI_Program " METHOD="GET or POST">
Form_Elements
</FORM>
The ACTION string identifies the program that will process the form input. In our
case, this will consist of a JSP program. The METHOD string specifies how data will be
sent to the server and is either GET or POST . GET means that the data will be sent as part
of the URL while POST means that the data will be sent as part of the data and will not
be visible in the URL.
Elements that can be inserted inside the form include selection lists, textboxes,
checkboxes, radio buttons, and several other common GUI widgets. In this section,
we will only introduce textboxes and submission buttons. A textbox has the
following syntax:
<INPUT TYPE="TEXT" NAME=" Textbox_Name " VALUE=" Default_Text "
SIZE = "Length_In._Characters"
MAXLENGTH=" Maximum_Number_Of_Allowable_Characters ">
All of the attributes are optional except for the input type. However, the name of
the textbox will be required to retrieve the textbox's value from JSP. If you would like
a default value to be entered in the textbox, you can put it in the VALUE field or leave it
blank for no text. The SIZE field controls the length of the textbox while MAXLENGTH
limits the number of characters that may be entered.
The submission button has the following syntax:
<INPUT TYPE="SUBMIT" NAME=" Name " VALUE=" Button_Text ">
The NAME field can be used to identify the submission button in case there are
multiple submission buttons and you wish to identify which one was clicked. VALUE
determines what text is placed in the button's label.
A sample form with two text fields and a submission button is shown in
Display 19.18. The form, when opened in a Web browser, is shown in Display 19.19.
It asks the user to enter an Author ID and a new URL with the intent of changing the
URL to the submitted value for the specified Author ID. Because we have not created
a JSP program that processes the form, the Web browser will display an error message
if the Submit button is clicked. We will write the JSP program in the next subsection.
 
Search WWH ::




Custom Search