Skip to content

HTML Forms

Handling The Client Request — Form Data

Concepts

  • Reading individual request parameters
  • Handling missing and malformed data

The Role of Form Data

Getting data from users.

  1. The HTML form tag.

    <form method="GET" action="/trivial" />
    
  2. The HTML input tags.

    <input type="text" name="firstName" value="Fred" />
    
    <input type="radio" name="options" value="type1" />
    <input type="radio" name="options" value="type2" />
    
  3. The Submit button.

    <input type="submit" value="Enter" />
    

Reading Form Data from Servlets

Reading Single Values

  • The HttpServletRequest class has methods for getting data from HTML forms
  • getParameter(“parameter”): This method gets one parameter. The name is case sensitive. The argument must match the “name” attribute on the form.
  • getParameterNames(): This method returns all the names of all form input tags as an Enumeration.
  • getParameterValues(“parameter”): This method returns all the values for inputs tags with the same name. An example is a select tag with the multiple attribute set.

Example

Missing and Malformed Data

  • If getParameter is called and the parameter is missing then the method will return null.
  • If getParameter is called and the user has not entered anything in the field then the method will return an empty String (i.e., "").