What I want:
If someone visits the page, could select two dates and click a (single) button for downloading the data between the two dates selected.
What I already have working:
JSP/web development is new for me. This is my scenario, using datepicker
and JSP have a page that make a query to the database, return certain data between two dates, and create a .txt
file with this data which saves locally.
Once the file is created, another button can be pressed to download the file.
What I need to work:
I need only one button who made both actions, so once the file is saved locally, a prompt for download appear and the visitor could make the download.
Because of how common the words are, it's hard to find what I need on search engines.
I need a button. 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
Info:
Apache Tomcat: 8.0.27 && Netbeans 8.1 && Ubuntu 14.04
JSP:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="resources/css/jtable.css" type="text/css">
<link rel="stylesheet" href="resources/css/style.css" type="text/css">
<link rel="stylesheet" href="resources/css/jquery-ui-1.10.3.custom.css" type="text/css">
<link href="resources/css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<script src="resources/js/jquery-1.11.3.js" type="text/javascript"></script>
<script src="resources/js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
<script src="resources/js/recuperacion-datos.js" type="text/javascript"></script>
<title></title>
<script>
$(document).ready(function(){
$("#datepicker1").datepicker({
maxDate: 0
});
$("#datepicker2").datepicker({
maxDate: 0,
onSelect: function(selected) {
$("#datepicker1").datepicker("option","maxDate", selected);
}
});
});
</script>
</head>
<body>
<center>
<div class="jtable-main-container" style="width: 60%;">
<div class="jtable-title">
<div class="jtable-title-text">
Recuperación de Datos
</div>
</div>
<div id="LecturasTableContainer" style="position: relative; text-align: center; font-size: 17px; top: 10px;">
Fecha Inicio: <input type="text" name="fechaInicio" id="datepicker1"> <span> </span><span> </span>
Fecha Fin: <input type="text" name="fechaFin" id="datepicker2">
<!-- Button who generate the file -->
<button type="submit" id="LoadRecordsButton" onclick="return confirm('Datos Recuperados');">Generar</button>
</div>
<br>
<!-- Button who download the file -->
<s:form action="download" method="POST">
<s:submit value="Descargar" type="button"/>
</s:form>
</div>
</center>
</body>
</html>
Action class for "Generar" Button:
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("mainopt", "dataRecovery");
return SUCCESS;
}
public String doDataRecovery() throws ParseException, FileNotFoundException{
DBControl dato = new DBControl();
fechaInicio = getFechaInicio();
fechaFin = getFechaFin();
DateFormat df = new SimpleDateFormat("yyyMMMddkkmm");
String fecha = df.format(Calendar.getInstance().getTime());
String lectura = dato.selecAlltLecturasFecha(fechaInicio, fechaFin);
File archivo = new File ("/media/recovery"+fecha+".txt");
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();
}
}
Action Class for "Descargar" Button:
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("yyyMMMddkkmm");
String fecha = df.format(Calendar.getInstance().getTime());
fileInputStream = new FileInputStream(new File ("/media/recovery"+fecha+".txt"));
return SUCCESS;
}
}
Struts.xml:
<action name="GetDataRecovery" class="com.raspberry.struts.action.DataRecovery"
method="doDataRecovery">
<interceptor-ref name="SessionValidationStack" />
<result name="success">main.jsp</result>
<result name="sessionexpired">index.jsp</result>
</action>
<action name="download" class="com.raspberry.struts.action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="recovery.txt"</param>
<param name="bufferSize">1024</param>
</result>
</action>