Read Client HTML Form data with getParameterNames()

Read Client HTML Form data with getParameterNames()


In the first simple program Login Screen Validation, the data sent by the client is extracted from HttpServletRequest object req, using getParameter(String) method. The getParameter(String) method works to read a single text box (or any FORM field) only. The HttpServletRequest interface comes with another method getParameterNames() (inherited from ServletRequest interface) to read all the FORM fields’ values at a time. Following is the method signature.

public abstract java.util.Enumeration getParameterNames();

The above signature indicates, the return value is an object of Enumeration interface. This method is used in this program.

Note: getParameter() reads only one HTML form field at a time.

Example on getParameterNames() where 5 form fields data is read at a time.

As usual there exists minimum two programs running one on client-side and the other on server-side.

1st Program (running on client): AllFields.html

Observe, the above HTML file comes with five text boxes from t1 to t5. The following servlet, reads all at a time.

2nd Program (running on server): ReadAll.java

web.xml entry for the above servlet:

String str1 = req.getParameter ("t3");

The above method is known earlier in the first program and with this method, we learnt how to read a single text box value. The same is repeated with t3 text box to know the value entered by the user in t3 box. But the aim of this code is different – to read all boxes’ values at a time.

Enumeration e = req.getParameterNames();

The getParameterNames() method returns an object of Enumeration interface. This Enumeration object e contains all the names of text boxes like t1, t2, t3 etc., but not their values. It is the same Enumeration interface we learnt in Collections framework. The object e contains all the text boxes names (t1 to t5) but not their values. It is extra following code to extract the values entered by the user in these form fields.

String name=(String) e.nextElement();
out.println(name + " : ");
String value=req.getParameter(name);
out.println(value.toUpperCase() + "<br>");

The Enumeration interface comes with two methods – hasMoreElements() returning a boolean value used to iterate the loop and the nextElement() returning an object of Object class used to retrieve the form field name.

The name variable contains the name of the FORM field (like t1 or t2 etc.). The getParameter(name) returns the value entered by the user in the FORM field name. The value variable contains the actual data entered by the user in the field name.

Using Enumeration object, all the values entered by the user in the FORM fields are extracted.

Following are output screens.

getParameterNames()

getParameterNames()

Observe the response screen. The values are not in the other of t1 to t5. It is the problem with Enumeration iteration and not with servlet. Anyhow, the order is not important here.

A similar program is available using Enumeration to read all the initialization parameters of web.xml file by a Servlet: Servlet web xml init param Example using ServletConfig.

Leave a Reply

Your email address will not be published. Required fields are marked *