title: Working with Java beans and Servlet values dynamically
body: <p>I have a table that sends a form post of 5 input text with values to servlet. In servlet I get the values and set them into bean class, and the sum that I set it too to bean class as sum.Then I get the values and the sum on the jsp table.The problem, is that now Im creating a button that when I click, I insert a row after the last row, and I want to keep the values too with the sum, How can I do it?</p>
  
  <p>This is my jsp that send the form to Servlet: <code>Test.jsp</code></p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;jsp:useBean id="helloBean" scope="request" class="user.HelloBean" /&gt;
  &lt;form method="post" action="Servlet"&gt;
      &lt;table id= "sum_table"&gt;
          &lt;tr&gt;
              &lt;td&gt;Test&lt;/td&gt;
              &lt;td&gt;Total&lt;/td&gt;
              &lt;td&gt;1&lt;/td&gt;
              &lt;td&gt;2&lt;/td&gt;
              &lt;td&gt;3&lt;/td&gt;
              &lt;td&gt;4&lt;/td&gt;
              &lt;td&gt;5&lt;/td&gt;
          &lt;/tr&gt;   
          &lt;tr&gt;
              &lt;td&gt;&lt;input type="text" value="Test1" maxlength=25&gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" value="${helloBean.sum}" maxlength=3 readonly="readonly"&gt;&lt;/td&gt;
              &lt;c:forEach items="${helloBean.values}" var="item"&gt;
                  &lt;td&gt;&lt;input type="text" id="val" name="values" value="${item}" maxlength=2/&gt;&lt;/td&gt;
              &lt;/c:forEach&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  
      &lt;p&gt;&lt;input type="submit"/&gt;&lt;/p&gt;
      &lt;input type="button" id="btnAdd" value="+"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Now I got the values on servlet, calculate the sum and send them to java bean: <code>Servlet.java</code></p>
  
  <pre><code>HelloBean hello  = new HelloBean();
  String[] values = request.getParameterValues("values");
  
  if (values == null) {
      values = new String[5];
  
      for(int i = 0; i &lt; 5; i++) {
          values[i]= "0";
      }
  }
  
  Integer sum = 0;
  
  for (int i = 0; i &lt; values.length; i++) {
      hello.setSum(sum = sum + Integer.valueOf(values[i]));
      hello.setValues(values);
  }   
  
  request.setAttribute("helloBean", hello);
  request.getRequestDispatcher("/Test.jsp").forward(request, response);
  </code></pre>
  
  <p>And this is my Bean Class: <code>HelloBean.java</code></p>
  
  <pre><code>public class HelloBean {
      String[] values;
      Integer sum;
  
      public Integer getSuma() {return sum;}
      public void setSum(Integer integer) {this.sum = integer;}
      public String[] getValues() {return values;}
      public void setValues(String[] val) {this.values = val;}
  }
  </code></pre>
  
  <p>This is how I add a new row:</p>
  
  <pre class="lang-javascript prettyprint-override"><code>$("#btnAdd").click(function () {
      var row = $("#sum_table tbody &gt; tr:last"),
          newRow = row.clone(true);
      newRow.find("input").each(function () {
          var num = +(this.id.match(/\d+$/) || [0])[0] + 1;
          this.id = this.id.replace(/\d+$/, "") + num;
          this.name = this.id;
      });
      newRow.insertAfter(row);
      return false;
  });
  </code></pre>
  
  <p>Thank You in Advance!</p>