0

Does anyone know how can I call HttpServlet or VaadinServlet on a button click?

The URL for UI is localhost:8181/OnlineAccounting/ I am able to call servlet by manually entering URL localhost:8181/OnlineAccounting/download but I want to achieve it with clicking a button.

package com.example.Reports;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

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 net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.view.JasperViewer;

import com.example.Connection.Connect;
import com.vaadin.server.VaadinServlet;

@WebServlet("download")
public class download extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public download() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


            Connect c=new Connect("{Call chartofaccounts()}", null);

            JRResultSetDataSource DataSet=new JRResultSetDataSource(c.rs);
            try{
            JasperReport JReport=JasperCompileManager.compileReport("C:\\Users\\mrreh_000\\Desktop\\Jasperreport\\MyReports\\Blank_A4_2.jasper");
            JasperPrint jprint;
            jprint = JasperFillManager.fillReport(JReport,null,DataSet);
            JasperViewer.viewReport(jprint,false);

    /// String source="C:\\Users\\mrreh_000\\Desktop\\Jasperreport\\MyReports\\Blank_A4.jrxml";
        //String query="{Call chartofaccounts()}";
          //      JReporting jReporting=new JReporting(source,query);

            String serverHomeDir = System.getenv("CATALINA_HOME");
            String reportDestination = serverHomeDir +"\\report.pdf";
            JasperExportManager.exportReportToPdfFile(jprint, reportDestination);

            FileInputStream fis = new FileInputStream(new File(reportDestination));

            // Fast way to copy a bytearray from InputStream to OutputStream
            org.apache.commons.io.IOUtils.copy(fis, response.getOutputStream());
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=" + reportDestination);
            response.flushBuffer();
            }catch(Exception e){System.out.println(e);}
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}
Opal
  • 81,889
  • 28
  • 189
  • 210

1 Answers1

0

Here a similar question on stackexchange.

I suggest that you also add a random part to your report filename (or the report path), otherwise the webbrowser will not redownload the report when it is already cached from a previous download.

Community
  • 1
  • 1
André Schild
  • 4,592
  • 5
  • 28
  • 42