title: PrimeFaces and JasperReports
body: <p>i'm trying to develop a web app for reporting, and have built a site with EJB3 + primefaces already. Now i'm head aching how to integrate Jasperreport into primefaces... I found no guide or discussion about this topic, I wonder is it not possible? or actually it is not a right way to do? I'm actually quite new to develop JavaEE6, dont plan to use frameworks like spring and seam yet. </p>
  
  <hr>
  
  <p><strong>model</strong></p>
  
  <pre><code>@Stateless
  @LocalBean
  public class BookEJB {
      // @Override
      public void printReport() throws ClassNotFoundException, IOException, JRException, SQLException {
          Connection connection;
          Map parameterMap = new HashMap();
  
          FacesContext ctx = FacesContext.getCurrentInstance();
  
          HttpServletResponse response = (HttpServletResponse) ctx
                  .getExternalContext().getResponse();
  
          InputStream reportStream = ctx.getExternalContext()
                  .getResourceAsStream("reports/report1.jasper");
  
          ServletOutputStream servletOutputStream = response.getOutputStream();
          Class.forName("com.mysql.jdbc.Driver");
          connection = DriverManager.getConnection("jdbc:mysql://localhost/bookdb?user=root&amp;password=******");
  
          ctx.responseComplete();
          response.setContentType("application/pdf");
  
          JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parameterMap, connection);
  
          connection.close();
          servletOutputStream.flush();
          servletOutputStream.close();
  
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@ManagedBean(name = "bookCtrl")
  @RequestScoped
  public class BookController {
  
      @EJB
      private BookEJB bookEJB;
  
      public void doPrintReport() throws ClassNotFoundException, IOException, JRException, SQLException {
          bookEJB.printReport();
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>view (JSF)</strong></p>
  
  <pre><code>&lt;body&gt;
  &lt;f:view&gt;
      &lt;h:outputText value="Click on the link below to generate the report." /&gt;
      &lt;h:form&gt;
          &lt;h:commandButton action="#{bookCtrl.doPrintReport}" value="Generate Report" /&gt;
      &lt;/h:form&gt;
  &lt;/f:view&gt;
  &lt;/body&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Jasper</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Created with Jaspersoft Studio --&gt;
  &lt;jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"&gt;
      &lt;property name="ireport.zoom" value="1.0"/&gt;
      &lt;property name="ireport.x" value="0"/&gt;
      &lt;property name="ireport.y" value="0"/&gt;
      &lt;queryString language="SQL"&gt;
          &lt;![CDATA[SELECT
       *
  FROM
       `book` book]]&gt;
      &lt;/queryString&gt;
      &lt;field name="Id" class="java.lang.Integer"/&gt;
      &lt;field name="title" class="java.lang.String"/&gt;
      &lt;field name="price" class="java.lang.String"/&gt;
      &lt;background&gt;
          &lt;band splitType="Stretch"/&gt;
      &lt;/background&gt;
      &lt;title&gt;
          &lt;band height="45" splitType="Stretch"/&gt;
      &lt;/title&gt;
      &lt;pageHeader&gt;
          &lt;band height="35" splitType="Stretch"/&gt;
      &lt;/pageHeader&gt;
      &lt;columnHeader&gt;
          &lt;band height="21" splitType="Stretch"&gt;
              &lt;staticText&gt;
                  &lt;reportElement x="0" y="0" width="100" height="20"/&gt;
                  &lt;textElement/&gt;
                  &lt;text&gt;&lt;![CDATA[Id]]&gt;&lt;/text&gt;
              &lt;/staticText&gt;
              &lt;staticText&gt;
                  &lt;reportElement x="100" y="0" width="100" height="20"/&gt;
                  &lt;textElement/&gt;
                  &lt;text&gt;&lt;![CDATA[title]]&gt;&lt;/text&gt;
              &lt;/staticText&gt;
              &lt;staticText&gt;
                  &lt;reportElement x="200" y="0" width="100" height="20"/&gt;
                  &lt;textElement/&gt;
                  &lt;text&gt;&lt;![CDATA[price]]&gt;&lt;/text&gt;
              &lt;/staticText&gt;
          &lt;/band&gt;
      &lt;/columnHeader&gt;
      &lt;detail&gt;
          &lt;band height="24" splitType="Stretch"&gt;
              &lt;textField&gt;
                  &lt;reportElement x="0" y="0" width="100" height="20"/&gt;
                  &lt;textElement/&gt;
                  &lt;textFieldExpression class="java.lang.Integer"&gt;&lt;![CDATA[$F{Id}]]&gt;&lt;/textFieldExpression&gt;
              &lt;/textField&gt;
              &lt;textField&gt;
                  &lt;reportElement x="100" y="0" width="100" height="20"/&gt;
                  &lt;textElement/&gt;
                  &lt;textFieldExpression class="java.lang.String"&gt;&lt;![CDATA[$F{title}]]&gt;&lt;/textFieldExpression&gt;
              &lt;/textField&gt;
              &lt;textField&gt;
                  &lt;reportElement x="200" y="0" width="100" height="20"/&gt;
                  &lt;textElement/&gt;
                  &lt;textFieldExpression class="java.lang.String"&gt;&lt;![CDATA[$F{price}]]&gt;&lt;/textFieldExpression&gt;
              &lt;/textField&gt;
          &lt;/band&gt;
      &lt;/detail&gt;
      &lt;columnFooter&gt;
          &lt;band height="45" splitType="Stretch"/&gt;
      &lt;/columnFooter&gt;
      &lt;pageFooter&gt;
          &lt;band height="54" splitType="Stretch"/&gt;
      &lt;/pageFooter&gt;
      &lt;summary&gt;
          &lt;band height="42" splitType="Stretch"/&gt;
      &lt;/summary&gt;
  &lt;/jasperReport&gt;
  </code></pre>
  
  <hr>
  
  <p>The error occurred when I click the button of JSF, below log is from glassfish</p>
  
  <pre><code>WARNING: A system exception occurred during an invocation on EJB BookEJB method public void blah.BookEJB.printReport() throws java.lang.ClassNotFoundException,java.io.IOException,net.sf.jasperreports.engine.JRException,java.sql.SQLException
  javax.ejb.EJBException.....
  
  SEVERE: javax.ejb.EJBException
  javax.faces.el.EvaluationException: javax.ejb.EJBException
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
  
  WARNING: #{bookCtrl.doPrintReport}: javax.ejb.EJBException
  javax.faces.FacesException: #{bookCtrl.doPrintReport}: javax.ejb.EJBException...
  
  Caused by: javax.faces.el.EvaluationException: javax.ejb.EJBException...
  
  SEVERE: javax.faces.FacesException: #{bookCtrl.doPrintReport}: javax.ejb.EJBException...
  
  Caused by: javax.faces.FacesException: #{bookCtrl.doPrintReport}: javax.ejb.EJBException...
  
  Caused by: javax.faces.el.EvaluationException: javax.ejb.EJBException...
  
  Caused by: javax.ejb.EJBException...
  
  Caused by: java.lang.NullPointerException...
  
  WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
  java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response.....
  
  WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
  java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response.....
  
  WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
  java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response.....
  </code></pre>