Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
386
Chapter 16 Using HTML Forms
URL of a program that will process the data, and when the user submits the
form (usually by pressing a button), the names and values of the controls are
sent to the designated URL as a string of the form
Name1=Value1&Name2=Value2...NameN=ValueN
This string can be sent to the designated program in one of two ways. The
first, which uses the HTTP
GET
method, appends the string to the end of the
specified URL, after a question mark. The second way data can be sent is by
the HTTP
POST
method. Here, the
POST
request line, the HTTP request
headers, and a blank line are first sent to the server, and then the data string
is sent on the following line.
For example, Listing 16.1 (HTML code) and Figure 16 1 (typical result)
show a simple form with two textfields. The HTML elements that make up this
form are discussed in detail in the rest of this chapter, but for now note a couple
of things. First, observe that one text field has a name of
firstName
and the
other has a name of
lastName
. Second, note that the GUI controls are consid
ered text level (inline) elements, so you need to use explicit HTML formatting
to make sure that the controls appear next to the text describing them. Finally,
notice that the
FORM
element designates
http://localhost:8088/Some
Program
as the URL to which the data will be sent.
Before submitting the form, I start a server program called
EchoServer
on port 8088 of my local machine.
EchoServer
, shown in Section 16.12, is a
mini Web server used for debugging. No matter what URL is specified and
what data is sent to it, it merely returns a Web page showing all the HTTP
information sent by the browser. As shown in Figure 16 2, when the form is
submitted with
Joe
in the first textfield and
Hacker
in the second, the
browser simply requests the URL
http://localhost:8088/Some
Program?firstName=Joe&lastName=Hacker
. Listing 16.2 (HTML code)
and Figure 16 3 (typical result) show a variation that uses
POST
instead of
GET
. As shown in Figure 16 4, submitting the form with textfield values of
Joe
and
Hacker
results in the line
firstName=Joe&lastName=Hacker
being sent to the browser on a separate line after the HTTP request headers
and a blank line.
That's the general idea behind HTML forms: GUI controls gather data
from the user, each control has a name and a value, and a string containing all
the name/value pairs is sent to the server when the form is submitted.
Extracting the names and values on the server is straightforward in servlets:
that was covered in Chapter 3 (Handling the Client Request: Form Data).
The remainder of this chapter covers options in setting up forms and the var
ious GUI controls you can put in them.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved