title: JasperReports PdfServlet to save report in PDF - how can I set the filename for the browser to prompt the user?
body: <p>Stack: <strong>JSF</strong> + <strong>PrimeFaces</strong> on <strong>JBoss AS</strong> with <strong>JasperReports</strong></p>
  
  <p>I have been using a pattern of exporting in PDF format using JasperReports with a three steps process:</p>
  
  <p>[1] obtain the compiled Jasper report from a path in the <strong>war</strong></p>
  
  <p>[2] place the <strong>JasperPrint</strong> object on the session</p>
  
  <p>[3] redirect to the URL of the <strong>PdfServlet</strong> </p>
  
  <p>So when the user from the GUI clicks on a <strong>p:commandButton</strong> a backing-bean's method is called that goes through [1], [2] and [3] as in the following example code:</p>
  
  <p><strong>xhtml file:</strong></p>
  
  <pre><code>&lt;p:commandButton ajax="false" action="#{indexController.exportPDF}" value="Export PDF"/&gt;
  </code></pre>
  
  <p><strong>backing bean code:</strong></p>
  
  <pre><code>private void putPrintObjectInSession() throws JRException {
      ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
      ServletContext context = (ServletContext) externalContext.getContext();
      String reportFileName = context.getRealPath("/reports/PrimeNumbersReport.jasper");
      File reportFile = new File(reportFileName);
      if (!reportFile.exists())
          throw new JRRuntimeException(".jasper file not found in the war.");
      Map parameters = new HashMap();
      parameters.put("ReportTitle", "2nd Prime Numbers Report");
      parameters.put("BaseDir", reportFile.getParentFile());
      JasperPrint jasperPrint = 
              JasperFillManager.fillReport(
                        reportFileName, 
                        parameters, 
                        getSQLConnection()
                      );
      ((HttpSession) externalContext.getSession(false)).setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
  }
  
  public String exportPDF() throws IOException, JRException {
      putPrintObjectInSession();
      FacesContext facesContext = FacesContext.getCurrentInstance();  
      ExternalContext externalContext = facesContext.getExternalContext();  
      externalContext.redirect("servlets/pdf");
      return null;
  }
  </code></pre>
  
  <p>I have two questions:</p>
  
  <p>[i] do you see any obvious code smells or limitations with this approach?</p>
  
  <p>[ii] with the example code above both Chrome and Conkeror can save the report but the default filename they present to the user for saving the file is simply "pdf". How can I configure that to a meaningful name (e.g. "report-2012-08-23c.pdf") ?</p>