title: How to: Select date, Press Button, Create File, Download File in Struts 2
body: <p>What I want:</p>
  <p>If someone visits the page, could select <em>two dates</em> and click a (single) <em>button</em> for <em>downloading</em> the <em>data</em> between the two dates selected.</p>
  <p>What I already have working:</p>
  <p>JSP/web development is new for me. This is my scenario, using <code>datepicker</code> and JSP have a page that make a query to the database, return certain data between two dates, and create a <code>.txt</code> file with this data which saves <em>locally</em>.</p>
  <p>Once the file is created, another button can be pressed to <em>download</em> the file.</p>
  <p>What I need to work:</p>
  <p>I need <em>only one button</em> who made <em>both actions</em>, so once the file is saved locally, a <em>prompt for download</em> appear and the visitor could make the download.</p>
  <p>Because of how common the words are, it's hard to find what I need on search engines.</p>
  <p><em>I need a button</em>. I don't want links or anchors that looks like a button, this could be an easy matter for some folks, but I already have lost two days in</p>
  <p><strong>Info:</strong></p>
  <p>Apache Tomcat: 8.0.27 &amp;&amp; Netbeans 8.1 &amp;&amp; Ubuntu 14.04</p>
  <p><strong>JSP:</strong></p>
  <pre class="lang-jsp prettyprint-override"><code>&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
  &lt;%@taglib prefix=&quot;s&quot; uri=&quot;/struts-tags&quot;%&gt;
  &lt;%@taglib prefix=&quot;sj&quot; uri=&quot;/struts-jquery-tags&quot;%&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
          &lt;link rel=&quot;stylesheet&quot; href=&quot;resources/css/jtable.css&quot; type=&quot;text/css&quot;&gt;        
          &lt;link rel=&quot;stylesheet&quot; href=&quot;resources/css/style.css&quot; type=&quot;text/css&quot;&gt;
          &lt;link rel=&quot;stylesheet&quot; href=&quot;resources/css/jquery-ui-1.10.3.custom.css&quot; type=&quot;text/css&quot;&gt;
          &lt;link href=&quot;resources/css/jquery-ui-1.10.3.custom.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt; 
          &lt;script src=&quot;resources/js/jquery-1.11.3.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
          &lt;script src=&quot;resources/js/jquery-ui-1.10.3.custom.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
          &lt;script src=&quot;resources/js/recuperacion-datos.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
          
          &lt;title&gt;&lt;/title&gt;
          &lt;script&gt;
              $(document).ready(function(){
                  $(&quot;#datepicker1&quot;).datepicker({
                      maxDate: 0
                  });
                  $(&quot;#datepicker2&quot;).datepicker({ 
                      maxDate: 0,
                      onSelect: function(selected) {
                         $(&quot;#datepicker1&quot;).datepicker(&quot;option&quot;,&quot;maxDate&quot;, selected);
                      }
                  });
              });
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;center&gt;
          &lt;div class=&quot;jtable-main-container&quot; style=&quot;width: 60%;&quot;&gt;
              &lt;div class=&quot;jtable-title&quot;&gt;
              &lt;div class=&quot;jtable-title-text&quot;&gt;
                  Recuperaci&amp;oacute;n de Datos
              &lt;/div&gt;
          &lt;/div&gt;
              &lt;div id=&quot;LecturasTableContainer&quot; style=&quot;position: relative; text-align: center; font-size: 17px; top: 10px;&quot;&gt;
             
                  Fecha Inicio: &lt;input type=&quot;text&quot; name=&quot;fechaInicio&quot; id=&quot;datepicker1&quot;&gt; &lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;
                  Fecha Fin: &lt;input type=&quot;text&quot; name=&quot;fechaFin&quot; id=&quot;datepicker2&quot;&gt;
                  
                  &lt;!-- Button who generate the file --&gt;
                  
                  &lt;button type=&quot;submit&quot; id=&quot;LoadRecordsButton&quot; onclick=&quot;return confirm('Datos Recuperados');&quot;&gt;Generar&lt;/button&gt;
              &lt;/div&gt;
                  &lt;br&gt;  
                  
                  &lt;!-- Button who download the file --&gt;
                  
                  &lt;s:form action=&quot;download&quot; method=&quot;POST&quot;&gt;
                      &lt;s:submit value=&quot;Descargar&quot; type=&quot;button&quot;/&gt;
                  &lt;/s:form&gt;                                    
          &lt;/div&gt;
      
      &lt;/center&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>Action class for &quot;Generar&quot; Button:</strong></p>
  <pre class="lang-java prettyprint-override"><code>package com.raspberry.struts.action;
  
  import static com.opensymphony.xwork2.Action.SUCCESS;
  import com.opensymphony.xwork2.ActionSupport;
  import com.raspberry.dao.control.DBControl;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.PrintWriter;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import org.apache.struts2.interceptor.ServletRequestAware;
  import java.sql.Connection;
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  
  public class DataRecovery extends ActionSupport implements ServletRequestAware {
      
      public HttpSession session;
      public Connection c;
      public String fechaFin;// = null;
      public String fechaInicio; // = null;
      
      public String goDataRecovery(){
          session.setAttribute(&quot;mainopt&quot;, &quot;dataRecovery&quot;);
          return SUCCESS;
      }
      
      public String doDataRecovery() throws ParseException, FileNotFoundException{  
          DBControl dato = new DBControl();
          
          fechaInicio = getFechaInicio();
          fechaFin = getFechaFin();      
          
          DateFormat df = new SimpleDateFormat(&quot;yyyMMMddkkmm&quot;);            
          String fecha = df.format(Calendar.getInstance().getTime());
                
          String lectura = dato.selecAlltLecturasFecha(fechaInicio, fechaFin);
          
          File archivo = new File (&quot;/media/recovery&quot;+fecha+&quot;.txt&quot;);
          archivo.getParentFile().mkdirs();
          PrintWriter printWriter;
          
          printWriter = new PrintWriter(archivo);
          printWriter.println (lectura);
          printWriter.close ();    
  
          return SUCCESS;        
      }
      
      public String getFechaFin() {        
          return fechaFin;
      }
      
      public String getFechaInicio() {
          return fechaInicio;
      }
      
      @Override
      public void setServletRequest(HttpServletRequest hsr) {
          session = hsr.getSession();
      }
  }
  </code></pre>
  <p><strong>Action Class for &quot;Descargar&quot; Button:</strong></p>
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.InputStream;
  import com.opensymphony.xwork2.ActionSupport;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  
  public class DownloadAction extends ActionSupport{
  
      private InputStream fileInputStream;      
  
      public InputStream getFileInputStream() throws Exception {           
              return fileInputStream;
      }
  
      public String execute() throws Exception {
              
              DateFormat df = new SimpleDateFormat(&quot;yyyMMMddkkmm&quot;);            
              String fecha = df.format(Calendar.getInstance().getTime());
              
              fileInputStream = new FileInputStream(new File (&quot;/media/recovery&quot;+fecha+&quot;.txt&quot;));
              
          return SUCCESS;
      }
  }
  </code></pre>
  <p><strong>Struts.xml:</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;action name=&quot;GetDataRecovery&quot; class=&quot;com.raspberry.struts.action.DataRecovery&quot; 
          method=&quot;doDataRecovery&quot;&gt;
      &lt;interceptor-ref name=&quot;SessionValidationStack&quot; /&gt;
      &lt;result name=&quot;success&quot;&gt;main.jsp&lt;/result&gt;
      &lt;result name=&quot;sessionexpired&quot;&gt;index.jsp&lt;/result&gt;            
  &lt;/action&gt;
  
  &lt;action name=&quot;download&quot; class=&quot;com.raspberry.struts.action.DownloadAction&quot;&gt;
      &lt;result name=&quot;success&quot; type=&quot;stream&quot;&gt;
        &lt;param name=&quot;contentType&quot;&gt;application/octet-stream&lt;/param&gt;
        &lt;param name=&quot;inputName&quot;&gt;fileInputStream&lt;/param&gt;
        &lt;param name=&quot;contentDisposition&quot;&gt;attachment;filename=&quot;recovery.txt&quot;&lt;/param&gt;
        &lt;param name=&quot;bufferSize&quot;&gt;1024&lt;/param&gt;
      &lt;/result&gt;
  &lt;/action&gt;
  </code></pre>