title: Go from JSP to Servlet, then come back to same JSP and the fields are empty
body: <p>I need to go from my JSP to my Servlet and then come back to the same JSP. The problem is that when i come back all my text fields are empty.</p>
  
  <p>How do i solve this? Notes:</p>
  
  <ol>
  <li>The form has <code>method='post' enctype='multipart/form-data'</code>.</li>
  <li>I'm using buttons to submit. They are normal buttons that execute a javascript function onclick, this function validates the text fields and if everything is ok then it calls to the <code>submit()</code> function and submits the form.</li>
  <li><p>The come back from the servlet is being done with this code:</p>
  
  <pre><code>RequestDispatcher rd = request.getRequestDispatcher("/altaPerfil.jsp");
  rd.forward(request, response);
  </code></pre></li>
  </ol>
  
  <p><br />
  <strong>Additional info:</strong><br /></p>
  
  <p>My JSP page is similar to this one: <a href="https://www.taringa.net/registro" rel="nofollow">https://www.taringa.net/registro</a>. <br />
  It has one form, inside the form there are 7 text fields, 3 select fields for the date(exactly like in that page), an upload field to upload an image, a radioButton with two fields and 3 buttons.<br />
  One of the buttons is used to validate the nick and email fields(if the nick or mail are already taken it returns false, otherwise returns true, this is done in the servlet because we try to keep the logic separated from the presentation). <br />
  The second button is used to upload the image.<br />
  The third button is used to send all the form(all the text fields and the date).<br />
  The first two buttons must go to the servlet and come back to the JSP...<br />
  <strong>Example code:</strong> <br />
  <strong>(JSP)</strong> <br /></p>
  
  <pre><code>&lt;form action="altaPerfilServlet" 
        name = "frmValidar" 
        method='post' enctype='multipart/form-data' &gt;
  
      Nick:
      &lt;br /&gt;
      &lt;input type="text" name="nick" 
             id="nick" onkeypress="deshabilita()" 
             value="${requestScope.nick}" /&gt;
  
  &lt;input type=button name="botonValidar" 
             id="botonValidar" value="Validar datos" 
             onclick="validarNick()" /&gt;
  &lt;!--Note: the javascript validarNick() verifies the nick field and if it is --&gt;
  &lt;!--not empty then it calls frmValidar.submit()--&gt;
  </code></pre>
  
  <p><br /></p>
  
  <p><strong>(Servlet)</strong><br /></p>
  
  <pre><code>  FileItemFactory factory = new DiskFileItemFactory();  
    ServletFileUpload upload = new ServletFileUpload(factory);
  
    List&lt;FileItem&gt; fields = upload.parseRequest(request);
    Iterator&lt;FileItem&gt; it = fields.iterator();
  
    &lt;!--here i process all the FileItems and obtain their values--&gt;
    &lt;!--then i talk to a java app (which acts as a server, providing --&gt;
    &lt;!--the data and a series of classes) and obtain a boolean value(esValido)--&gt;
    &lt;!--that tells me if the nick is available(true) or taken(false)--&gt;
  
    &lt;!--finally: --&gt;
  
    request.setAttribute("esValido", esValido);&lt;!-- saving the boolean --&gt;
  
    String nick=request.getParameter("nick");
    request.setAttribute("nick", nick);
    RequestDispatcher rd = request.getRequestDispatcher("/altaPerfil.jsp");
    rd.forward(request, response);
  </code></pre>