servletOutputStream, new HashMap(), csvDataSource);
catch (Exception e)
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
Since the constructor for CSVDataSource takes an instance of java.io.
Reader as its only argument, and javax.servlet.ServletContext.
getResourceAsStream() returns a java.io.InputStream , we take advantage of the
java.io.InputStreamReader class. This class takes an InputStream and converts
it to a Reader . We then pass this Reader to CSVDataSource , and proceed just as
we have in previous examples. This example assumes that the CSV file is saved as
This chapter has given us a quick run through all the non-database datasources
supported by JasperReports, including how to create our own.
We have created reports that use no external datasources by using an empty
datasource and have also used instances of a class implementing java.util.Map
as a datasource by taking advantage of the net.sf.jasperreports.engine.data.
JRMapArrayDataSource class. We learned to use plain Java objects as datasources
by employing the net.sf.jasperreports.engine.JRBeanArrayDataSource
and net.sf.jasperreports.engine.JRBeanCollectionDataSource classes.
Besides, we also saw the use of a Swing TableModel and an XML document
as a datasource by implementing the net.sf.jasperreports.engine.data.
JRTableModelDataSource and net.sf.jasperreports.engine.data.
JRXmlDataSource classes respectively.
We have covered not only the datasources supported by JasperReports, but also created
custom datasources by creating our own JRDataSource implementation. In addition
to datasources, we also discussed how to pass data in the form of report parameters.