Java Reference
In-Depth Information
return reportRows;
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
ServletOutputStream servletOutputStream = response
getOutputStream();
InputStream reportStream = getServletConfig().getServletContext()
.getResourceAsStream("/reports/AircraftReport.jasper");
try
{
JRDataSource dataSource = createReportDataSource();
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), dataSource);
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
}
catch (Exception e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
}
The
JRMapArrayDataSource
class has a single public constructor. This constructor
takes an array of
Map
objects as its only argument. The array must already
contain the maps to be used to populate the report before we pass it to the
JRMapArrayDataSource
object.
Map
keys must map field names in the report
template. This way the JasperReports engine knows what values to use to populate
the report template's fields.
In addition to allowing us to use arrays of maps as datasources, JasperReports also
allows us to use a
Collection
of
Map
objects as a datasource. JasperReports provides
an implementation of
JRDataSource
that we can use for this purpose. It is called
net.sf.jasperreports.engine.data.JRMapCollectionDataSource
. Using this