title: Passing form input values as a List of objects from a JSP page to a Servlet
body: <p>Here is a <strong>simplified</strong> (NOT real world) example. Suppose there is a domain model - a class <em>Movie</em>, which has a List of actors. A class <em>Actor</em> has three fields (<em>name</em>, <em>birthDate</em>, <em>rolesNumber</em>). The following code is an illustration of this scenario:</p>
  
  <p><em>Movie.java</em></p>
  
  <pre><code>public class Movie {
  
      // some fields
  
      private List&lt;Actor&gt; actors;
  
      // getters and setters
  }
  </code></pre>
  
  <p><em>Actor.java</em></p>
  
  <pre><code>public class Actor {
  
      private String name;
  
      private Date birthDate;
  
      private int rolesNumber;
  
      // getters and setters
  } 
  </code></pre>
  
  <p><br>
  There is also a JSP page where we output in a loop the information about every actor that plays in a concrete movie and the user can <strong>update the corresponding text field values</strong> and submit changes to a servlet: </p>
  
  <p><em>actorsUpdate.jsp</em></p>
  
  <pre class="lang-default prettyprint-override"><code>...
  &lt;c:forEach items="${movie.actors}" var="actor"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;td&gt;&lt;input type="text" value="${actor.name}" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th&gt;Birth Date&lt;/th&gt;
              &lt;td&gt;&lt;input type="text" value="${actor.birthDate}" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th&gt;Number of Previous Roles&lt;/th&gt;
              &lt;td&gt;&lt;input type="text" value="${actor.rolesNumber}" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;hr /&gt;
  &lt;/c:forEach&gt;
  ...
  </code></pre>
  
  <p><br>
  It is known that in order to retrieve text fields in a servlet, one can use <em>ServletRequest's</em> methods like <a href="http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29" rel="nofollow"><code>getParameter()</code></a> or <a href="http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getParameterValues%28java.lang.String%29" rel="nofollow"><code>getParameterValues()</code></a> etc. But how to retrieve the updated input fields as a List of objects (so that every three related values were grouped)?</p>
  
  <p>If it were Spring project we could use Spring's <code>&lt;form:form modelAttribute="modelName"&gt;</code><br>
  tag and have a <strong>backing object</strong> defined in the <em>modelAttribute</em>. But how about pure JSP/Servlet project?  </p>
  
  <h1>Possible solution</h1>
  
  <p>One of the possible solutions is to assign names in the "<em>name</em>" attribute of the text inputs and append <em>varStatus.index</em>, like this:</p>
  
  <pre class="lang-default prettyprint-override"><code>&lt;c:forEach items="${movie.actors}" var="actor" varStatus="counter"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;td&gt;&lt;input type="text" name="name${counter.index}" value="${actor.name}" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          ...
      &lt;/table&gt;
      &lt;hr /&gt;
  &lt;/c:forEach&gt;
  </code></pre>
  
  <p>So this index would allow us to <strong>identify values related to ONE object</strong>. And we could also generate some hidden input field where we could store a <em>loop count</em> (a number of actors), and then in a servlet we could retrieve the values related to one object like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>List&lt;Actor&gt; actors= new ArrayList&lt;Actor&gt;();
  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  for (int i = 0; i &lt; count; i++) {
      String name= request.getParamater("name" + i);
      Date birthDate = dateFormat.parse(request.getParamater("birthDate" + i));
      int rolesNumber = Integer.parseInt(request.getParamater("rolesNumber" + i));
  
      Actor actor = new Actor(name, birthDate, rolesNumber);
      actors.add(actor);
  }
  </code></pre>
  
  <hr>
  
  <h2>My questions are:</h2>
  
  <ol>
  <li><p>Is there another, more elegant and effective way of passing the updated text field values to a servlet as a <strong>List of objects</strong>?</p></li>
  <li><p>Is there any solution <strong>similar or equivalent</strong> to that of the Spring's <code>&lt;form:form&gt;</code> tag in a world of pure JSP/JSTL/EL/Servlets?</p></li>
  </ol>
  
  <hr>
  
  <h1>UPDATE</h1>
  
  <p>Looks like nobody knows the answer to the above questions. The accepted answer is not exactly what I asked for (see my comments to it).</p>
  
  <p>There seems to be no equivalent of Spring's <code>&lt;form:form modelAttribute&gt;</code> tag or something similar in the world of pure Servlets/JSP.</p>
  
  <p>Well, full <a href="http://www.oracle.com/technetwork/java/javaee/overview/index.html" rel="nofollow">Java EE</a>, or <a href="http://projects.spring.io/spring-framework/" rel="nofollow">Spring Framework</a>, <a href="http://struts.apache.org/" rel="nofollow">Apache Struts</a> or another powerful <a href="http://en.wikipedia.org/wiki/Web_application_framework" rel="nofollow">web application framework</a> to the rescue! </p>