Using Forms to Obtain a User's Input
After showing a Web page, the servlet must be able to obtain the
user's input in order to process the next servlet request. This
is usually done using forms, which can display
window controls such as text fields or push buttons. When the user
performs the action associated with the form (usually a push button),
the servlet is called again with this action and input parameters
taken from text fields, check boxes and other input controls.
By specifying method=get (at 1 ),
the doGet() method of the servlet will be
called. However:
- You could also specify method=post, which would cause the doPost() method to be called.
- The difference between method=get and method=post is that:
- Using doGet() will display the generated servlet invocation string in the Web browser's address/location field.
- Using doPost() will suppress the Web browser's address/location field.
- You are recommended to use doPost() if the form contains any password fields. Passwords will then appear as clear text, when using doGet().
The HTML code shown in Figure 1 will display the
window controls shown below:
If the user presses the push-button Order It!,
this servlet invocation string will be generated:
http://computername/servlet/FlightOrderingServlet?action=order3&flight=34
&firstname=name1&lastname=name2&seats=1&nonsmoke=no
where:
- name1 is the string that was entered in the firstname field.
- name2 is the string that was entered in the lastname field.
- computername is the name of your workstation in your network, or its IP address.