ASPECTOS GENERALES DEL BOSQUE DE PROTECCION CORDILLERA ESCALERA
CORDILLERA ESCALERA
HTML forms allow us to gather complex information from users of our web applications. The use of forms is ubiquitous on the Web. Without support for forms, the Web would never have exploded; limited to just hyperlinks, it would likely have stayed as passive, static hypertext—an interesting toy, but with no possibility of creating an Amazon or an eBay. Without forms, there would be no way to build any of the interesting e-commerce, content management, or com- munity sites.
HTML forms are significantly different from forms within a desktop applica- tion. In a desktop application, each component (text field, check box, push but- ton) interacts with the application completely independently of the others. The user may update one field without changing the value for another field. The code for a desktop application is informed of every key press or mouse click, a luxury not possible with HTML forms. HTML forms are displayed to the user, who may use the mouse or the Tab key to move between the form controls and enter values. When the user clicks the submit button, the values for all of the
fields and controls within the form are packaged together in a single request to the server. The server sees only the final result, not the individual mouse move- ments and key presses, or even the individual changes to fields.
Within an HTML form, various HTML elements work together. The outer- most element, <form>, groups together the many other elements it encloses
between its start and end tags. Other elements within <form> are used to create
types of form controls; these are listed in table 3.1.
Each of these form control elements has a name attribute. When the form is sub-
mitted, the value for each control is submitted as a query parameter using the control’s name. Figure 3.1 shows how this comes together; when the page con- taining the form is rendered, a name is assigned to each element. Once the form is submitted, these values are provided in the form submission and available through the Servlet API’s HttpServletRequest object. Figure 3.1 shows a single
form; beginning at the top center, the form starts as HTML: <form> and <input>
tags. The client web browser uses these to construct form controls (radio buttons, text fields, and so forth) for the user to directly interact with. When the user fin- ishes entering information and clicks the submit button, the request includes the values for those fields as query parameter values. Within the Servlet API, those query parameters are accessible via the HttpServletRequest object.
Table 3.1 Elements used to create form controls
HTML element Control type <input type="checkbox"> Check box (toggles on or off)
<input type="radio"> Radio selection (user selects one value from a list of values) <input type="text"> Simple text input field
<input type="password"> Password field (text field where user input is obscured) <input type="hidden"> Hidden field (not visible to the user)
<input type="submit"> Form submit button; sends the request to the server <input type="reset"> Reset button; returns all fields to initial values
<input type="image"> Image map field; submits form and identifies where, within the image, the user clicked
<input type="file"> File upload
<select> Drop-down list or multiple-selection list <textarea> Multiline text input field
Understanding HTML forms 95
In a servlet application, the next step is to access those query parameters and use them to update domain object properties. (This may involve some conversions, such as parsing string values into numbers.) One of the stumbling blocks when you’re using servlets is ensuring that the form control names used in the Java- Server Page (JSP) match the query parameter names referenced in the servlet code. With servlets, you are responsible for coming up with appropriate names and keeping the JSP and the servlet synchronized with respect to those names. Expecting form control names in the JSP to stay synchronized with Java code in a servlet is an example of weak binding (discussed in chapter 1) between the JSP
and the servlet.
Tapestry adds a layer beyond the query parameters—a layer of components. The form control components read page properties when rendering the initial
HTML. The same components come back into play when the form is submitted to read the query parameters and update the page properties.
The framework takes care of all the mundane plumbing: providing names for form control elements, writing HTML, and reading query parameters. There is never a danger of the HTML template for a form getting out of synchronization
Figure 3.1 The form and the data it contains are represented in different ways at different times. The form starts as HTML sent via HTTP to the web browser. The web browser displays controls the user can interact with. The form submission contains the values entered by the user, which finally show up as Java objects.
with the code that handles the form submission because that logic is distributed into the very components that render the form’s HTML in the first place.
All of this means that by the time your form’s listener method is invoked, the components will have already updated all of your domain objects’ properties to match the values entered by the user. The listener method can simply use those properties to accomplish the operation the form is intended for, whatever that may be.
To appreciate how all these concepts fit together, we’ll start with a simple exam- ple before getting into the nitty-gritty of the individual Tapestry components.