如何使用servlet响应打开新选项卡?

时间:2015-06-17 10:21:51

标签: java jsp servlets jasper-reports jsp-tags

我想在Web应用程序中运行index.jsp文件时打开一个新选项卡。我使用Show.java(一个servlet)来执行它。目前的情况是,当我点击Print中的index.jsp按钮时,我想在新标签页中打开的内容页面显示在当前标签上。我想使用servlet响应,而不是使用javascriptjquery

这是我用index.jsp文件写的,

<form action="Show">            
    <input type="submit" value="Print"/>
</form>

<%@page import="net.sf.jasperreports.engine.export.JRPdfExporter" %>

<%
   JRPdfExporter export = (JRPdfExporter)request.getAttribute("exportIndentObject");
   if (export != null){
         JRPdfExporter export1 = (JRPdfExporter) session.getAttribute("exportIndentObject");
         export1.exportReport();
    }%>

这是Show.java我用来保存内容,

String param ="Terance";     
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("param", param);                
  JRPdfExporter jRPdfExporter = new  JRPdfExporter();                                                                       jRPdfExporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT,                                      this.print();");

  String jsaperFile="C:\\Users\\user\\Desktop\\Terance\\Jasper\\JasperReport\\src\\java\\jasper\\Report.jrxml";
System.out.println(jsaperFile);
try
                    {
                        String report=JasperCompileManager.compileReportToFile(jsaperFile);

                        File file=new File(report);
                        if(!file.exists()){
                            throw new JRException("Cannot find the given file .");
                        }

                        JasperPrint jasperPrint=JasperFillManager.fillReport(report, map);
                        byte[] exportReportToPdf = JasperExportManager.exportReportToPdf(jasperPrint);

                        ServletOutputStream sout = response.getOutputStream();
                        response.setContentType("application/pdf;");

                        jRPdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                        jRPdfExporter.setParameter(JRExporterParameter.OUTPUT_WRITER, sout);
                        jRPdfExporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();");

                        request.setAttribute("exportIndentObject", jRPdfExporter);

                        response.setContentType("application/pdf");
                        response.setHeader("Content-disposition","inline; filename=\"myReport.pdf\"");        


                        sout.write(exportReportToPdf);      
                        sout.flush();
                        sout.close();              

                    } catch (JRException ex)
                    {
                        ex.printStackTrace();
                    }

当我使用response.redirect();时,我甚至无法在浏览器中获取index.jsp文件。

2 个答案:

答案 0 :(得分:4)

仅限全部或全部:

$this->header = getallheaders();
$this->uid = (isset($this->header['uid']) && !empty($this->header['uid'])?$this->header['uid']:null);

echo $this->uid; //return the User Id sent via Post request from Cordova FileTransfert.

因此,可以将打印按钮放在原始表单之外,并拥有自己的表单标记。

答案 1 :(得分:2)

Servlet在服务器端工作,并为您的浏览器生成http响应,但它无法响应任何打开新选项卡的命令。这取决于用户浏览器设置(打开新窗口或新选项卡),因此Servlet无法执行此操作。