vaadin servlet按钮调用

时间:2015-05-25 19:35:10

标签: java servlets vaadin vaadin7

是否有人知道如何在点击按钮时拨打HttpServletVaadinServlet

用户界面的网址为localhost:8181/OnlineAccounting/ 我可以通过手动输入URL localhost:8181/OnlineAccounting/download来调用servlet,但我希望通过单击按钮来实现它。

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
    }

}

1 个答案:

答案 0 :(得分:0)

Here关于stackexchange的类似问题。

我建议你还在报告文件名(或报告路径)中添加一个随机部分,否则当网页浏览器已经从之前的下载缓存时,它不会重新下载报告。