Java Reference
In-Depth Information
Class.forName("com.mysql.jdbc.Driver");
"com.mysql.jdbc.Driver");
com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:
jdbc:mysql://localhost:
3306/flightstats?user=user&password=secret");
"jdbc:mysql://localhost:
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), connection);
connection.close();
response.setContentType("application/pdf");
application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
"application/pdf");
}
}
JasperReports does not natively integrate with JSF. Therefore, the trick to getting
them to work together is to use the JSF API to obtain objects from the servlet API,
since JasperReports integrates nicely with these objects. The HttpServletResponse
object is of particular interest.
As can be seen in the preceding example, we can obtain the HttpServletResponse
object by calling the FacesContext.getExternalContext().getResponse()
method. We can then obtain an instance of ServletOutputStream from the response
object as usual, by calling the HttpServletResponse.getOutputStream() method.
In order to know what report to compile, we need to load the compiled report
template as a stream. This is accomplished by a call to the FacesContext.
getExternalContext().getResourceAsStream() method, as illustrated in the
code. For this example, we will use the report template discussed in the Embedding
SQL Queries into a Report Template section of Chapter 4.
Once we have obtained the HttpServletResponse object and the
ServletOutputStream object, and loaded the report template into memory,
then all we need to do to generate the report is execute the JasperRunManager.
runReportToPdfStream() method, passing the appropriate parameters. This,
of course, assumes that we want to render the report as a PDF; to render in other
formats, we need to substitute with the appropriate call.
 
Search WWH ::




Custom Search