0

How will I pass the data as an ArrayList into my servlet?

<Table id="table1">
     <tr>
          <td>
              <input type="hidden" value="test1_1" name="name1_1" />
          </td>
         <td>_
              <input type="hidden" value="test2_1" name="name2_" />
          </td>
         <td>
              <input type="hidden" value="test3_1" name="name3_1" />
          </td>
     </tr>
     <tr>
          <td>
              <input type="hidden" value="test1_2" name="name1_2" />
          </td>
         <td>
              <input type="hidden" value="test2_2" name="name2_2" />
          </td>
         <td>
              <input type="hidden" value="test3_2" name="name3_2" />
          </td>
     </tr>
     <tr>
          <td>
              <input type="hidden" value="test1_3" name="name1_3" />
          </td>
         <td>
              <input type="hidden" value="test2_3" name="name2_3" />
          </td>
         <td>
              <input type="hidden" value="test3_3" name="name3_3" />
          </td>
     </tr>
</Table>




List<Model> newList = new ArrayList<Model>();

The servlet newList must contains 3 Model

Model1 with value of [getSomething1 = test1_1, getSomething2 = test2_1, getSomething3 = test3_1]
Model2 with value of [getSomething1 = test1_2, getSomething2 = test2_2, getSomething3 = test3_2]
Model3 with value of [getSomething1 = test1_3, getSomething3 = test2_3, getSomething3 = test3_3]

And this will not limit to 3 Model in a list but will depends on the number of row.

It will create a number of models based on the number of row in the JSP.

2
  • 2
    If the hidden input fields are submitted as part of a form to the correct URL, you should get them as part of the HttpServletRequest parameters Commented Mar 24, 2014 at 20:37
  • you can pass the data in json fromat to servlet and use gson to get Java object from json string. Commented Mar 25, 2014 at 5:44

1 Answer 1

1

You can't pass it as an ArrayList, but you can read it as a String[] if you give all the inputs you want in the array the same name.

 <input name='test' />
 <input name='test' />
 <input name='test' />

In your servlet, use request.getParameterValues(name) (which returns a String[]) rather than request.getParameter(name) which only returns one value.

 String[] testValues = request.getParameterValues("test");
Sign up to request clarification or add additional context in comments.

1 Comment

Just to add on what david has said, you can also use request.getParamterMap(); which will return Map<String, String[]> where the key is the name of the parameters and the string array are your values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.