tags:

views:

171

answers:

5

I am trying to submit a form to a servlet (java). The form has a bunch of questions, each question has 4 radio buttons and the user selects one of them. I do not know the number of questions that will be in the form. It could be 10, 15, 12...depends on some other criterias. My question is what is the best way to retrieve the list of choices that the user made for the questions on the form.

A: 

A typical technique is to name the fields with a common prefix, and then iterate through them: q000, q001, q002, etc., until you reach one that's not present.

Curt Sampson
+1  A: 

A quick trick that comes to my mind is to name all the fields as

"question_"+n

And have a input type hidden with the value of n. If the form has a way to know how many question to present, it should have a way to set the value of n.

Later you just retrieve that value and ...

 n = new Integer( request.getParameter("number_of_question"));
 for( int i = 0 ; i < n ; i++ ) { 
      list.add( request.getParameter("question_"+i));
  }

This is the first thing that comes to my mind

OscarRyz
+1  A: 

You can use HttpServletRequest.getParameterNames() to retrieve an Enumeration of names of all form elements within the request. You can then iterate over the enumeration and request individual value for each element using HttpServletRequest.getParameter(name).

If your HTML contains other FORM elements other than your option radio buttons, use clever naming convention of these radio button so that while enumerating over the parameter names, you know what to request.

An example.

If your form contains two questions with following options:

Question 1: 
<input type="radio" name="question1" value="option1">
<input type="radio" name="question1" value="option2">
<input type="radio" name="question1" value="option3"> 

Question 2:
<input type="radio" name="question2" value="option1">
<input type="radio" name="question2" value="option2">
<input type="radio" name="question2" value="option3">

In your servlet,

Enumeration e = request.getParameterValues();
while(e.hasMoreElements()){
     String name = (String)e.nextElement();
     if(name.startsWith("question"){
        String value = request.getParameter(name);
        //your logic here
     }

}

Another of doing the same is:

In your servlet,

int maxQuestionNumber = Integer.parseInt(request.getParameter("maxQuestionNumber"));//this should be a hidden variable in your HTML form representing the max questions in your form.

for(int i=1;i<=maxQuestionNumber;i++){
     String value = request.getParameter("question"+i);
     //your logic here..
}
Suresh Kumar
+1  A: 

I would suggest no workarounds. the ServletRequest.getParameterMap() would come handy in this scenerio. The keys of the map would be of type String, and the values will be of type String[].

Hence, you can very easily loop through the map using foreach loop something like this,

for(Map.Entry<K, V> entry : map.entrySet()){
    ..
    if(entry.getValue().length > 1){
       //means the choices not the question
    }else{
       //means the question not the choices
    }
}

I hope it would help.

Adeel Ansari
A: 

You could just use JSON, and so pass a string to the servlet, which can handle a very long string if you use POST. That way you can pass whatever you want, even if the parameters are complex.

James Black