title: Updating the table created in JSP using the values sent from the form (No database)
body: <p>I have two files. One is the index.jsp and other is the users.java.</p>
  
  <p>In index.jsp, I have two text fields to accept the username and phone number. This is sent to users.java servlet.There the values are stored in the session and is sent back to the same index.jsp using RequestDispatcher. I need these values to be shown in a table and keep updating this table as the form is submitted with newer values. </p>
  
  <p>So far I can get the values back to the table, but when a new form is submitted , the old row is replaced with a new row resulting in getting a table of only one row. 
  How to update the table by appending the rows?</p>
  
  <p>Here is the index.jsp</p>
  
  <pre><code>    &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;PRODUCTS&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action=sample method="POST"&gt;
              &lt;pre&gt;User Name        Phone Number&lt;/pre&gt;
              &lt;pre&gt;&lt;input type="text" name="userName"&gt;    &lt;input type="text" name="phoneNumber"&gt;&lt;/pre&gt;&lt;br&gt;
              &lt;input type="submit" value="submit"&gt;
      &lt;/form&gt;
  
          &lt;pre&gt;
              &lt;table id=prod border=1&gt;
              &lt;tr&gt;
              &lt;th&gt; User Name&lt;/th&gt;
              &lt;th&gt; Phone Number &lt;/th&gt;
              &lt;/tr&gt;
              &lt;div id="output"&gt;
                      ${requestScope.Output }
              &lt;/div&gt;
              &lt;/table&gt;
          &lt;/pre&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is the user.java code ( I now that the values must be stored in a session so that it can be used later. Not sure how to retain the old row in the table)</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  @WebServlet("/sample")
  public class sample extends HttpServlet {
      private static final long serialVersionUID = 1L;
      public sample() {
  
          super();
          // TODO Auto-generated constructor stub
      }
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      }   
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              HttpSession session=request.getSession();
              String userName = request.getParameter("userName");
              String phoneNumber= request.getParameter("phoneNumber");
              session.setAttribute("uName",userName);
              session.setAttribute("phNo",phoneNumber);
              StringBuffer Output= new StringBuffer();
  
              String add="&lt;tr&gt;&lt;td&gt;"+session.getAttribute("uName")+"&lt;/td&gt;&lt;td&gt;"+session.getAttribute("phNo")+"&lt;/td&gt;&lt;/tr&gt;";
              Output.append(add);
              request.setAttribute("Output",Output);
              request.getRequestDispatcher("/index.jsp").forward(request, response);
  
  
  
      }
  </code></pre>
  
  <p>I get the table with one row having the user name and phone number. But when I enter a new user name and phone number and submit the form, the table will replace the old row with new row. I need to append the rows. How to do it?</p>