Read Client Form Data JSP 3 Ways Example

request is an object of HttpServletRequest and is one of the 9 implicit objects, JSP supports. request object is used to retrieve client’s system (like IP address) information, client’s browser (header) information and the data entered by the client in the <form> fields.

To retrieve what the client entered in the <form> fields, request object comes with 3 methods getParameter(String), getParameterNames() and getParameterValues(String).

Let us see what Java API says about these methods as defined in javax.servlet.ServletRequest interface and inherited by HttpServletRequest interface.

  • public java.lang.String getParameter(java.lang.String name): Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request.
  • public java.util.Enumeration getParameterNames():
    Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.
  • public java.lang.String[] getParameterValues(java.lang.String name):
    Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist. If the parameter has a single value, the array has a length of 1.
Examples on Read Client Form Data JSP

1. Using getParameter(String)

This method is used to read only one form field data as a string. Example is available at First JSP Example User Name and Password Login Validation. Anyhow this method usage is shown in the next 2nd way.

2. Using getParameterNames()

This method is used to read all form fields data at a time as an Enumeration object.

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

Reading all Text box values at a time

Enter First Name
Enter Middle Name
Enter Last Name
Enter Experience
Enter Hobby

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

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

<%				// to retrieve one text box value
    String str1=request.getParameter ("t3");
    out.println("Reading only one t3 field value : " + str1 + "

"); // to retrieve all fields values at a time out.println("Reading all text boxes values at a time:
"); Enumeration e = request.getParameterNames(); out.println(""); while(e.hasMoreElements()) { String name=(String) e.nextElement(); out.println( name +" : "); String value=request.getParameter(name); out.println(value.toUpperCase() +"
"); // just for a change, values are printed in uppercase letters } %>

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

The getParameter() method reads only one form field data of what the user entered in t3 field and is returned as string, here str1. But the aim of this code is different – to read all boxes’ values at a time.

Enumeration e = request.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.

Read Client Form Data JSP

Read Client Form Data JSP

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.

3. Using getParameterValues(String)

getParameterValues(String) is used to read multiple form field data as a string array where fields have some common name. .

Observe the following client HTML file, StudentAddress.html

  
Enter Name
Enter ID
Enter H.No.
Enter Street
Enter Area
Enter City
Enter PIN

Observe, there are total 7 form fields of which five fields are having a common name of t3. All the five form fields with t3 name comprises of a student address. The job of the JSP may be to extract all the 7 form fields data and enter into a database table. Imagine, the database may have only 3 columns like stdname, stdid and stdaddress. The value of t1 goes into the stdname column, t2 goes into stdid and all the remaining five fields with name t3 goes into stdaddress.

How to do this job easily in a JSP?

Read t1 and t2 separately with getParameter(String) method as usual as you did in What is Scriptlet with Example Login Form?. Now comes our getParameterValues(String) method to read all the t3 values at a time. This method returns a string array containing all the values entered by the user in t3 fields. Take this array and enter into the database column. Ofcourse, the aim of this JSP is reading only but does not do any database operation.


<%
    String address[] = request.getParameterValues("t3");
 
    StringBuffer temp = new StringBuffer();
    for(String str : address)
    {
      temp.append(str + ", ");
    }        

    out.println(temp);
%>


getParameterValues(String) avoids reading separately. But the condition is all the fields should have the common name. The StringBuffer object temp can be taken and inserted (after converting to string) into a database column.

Client-side HTML screen when values are entered.

Read Client Form Data JSP

Output screen when the submit button is clicked.

Read Client Form Data JSP

Leave a Comment

Your email address will not be published.