How open a new tab using servlet response?

12.8k views Asked by At

I want to open a new tab when I run index.jsp file in a web application . And I have used Show.java (a servlet) to perform that . The current situation is when I click the Print button in index.jsp , the content page which I want to open in a new tab is displaying on the current tab . I want to do it using servlet response and not using javascript or jquery .

This is the I've written in index.jsp file ,

<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();
    }%>

This is Show.java which I use to keep content,

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();
                    }

When I use response.redirect(); , I couldn't get even the index.jsp file in browser .

2

There are 2 answers

1
P_M On BEST ANSWER

Servlet works on server side, and generate http response to your browser, but it cannot response with any command to open new tab. This depends on user browser settings (to open new window or new tab), so Servlet cannot do this.

1
Joop Eggen On

Only for all or none:

<form ... target="_blank">

So maybe place the print button outside the original form and have its own form tags.