Here are the example codes that I extracted from our application. We have a servlet to launch the report. I extracted the codes required to generate the jasper report. You must of course populate the bean (MyJavaBean) first.
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
MyJavaBean bean;
String reportFileName = "C:/TEMP/case/forms/MYREPORT.jasper";
String jrxmlName = "C:/TEMP/case/forms/MYREPORT.jrxml";
//... populate the bean here before proceeding
//bean is ready, fill the report with the bean
JasperPrint jasperPrint9 = null;
JasperCompileManager.compileReportToFile(jrxmlName);
HashMap<String, Comparable> params = new HashMap<String, Comparable>();
//params.put("case_id",new Integer(0)); // need this only if your report accept parameters
ArrayList<MyJavaBean> beanCollection = new ArrayList<MyJavaBean>();
beanCollection.add(bean);
JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(beanCollection);
jasperPrint9 = JasperFillManager.fillReport(reportFileName, params, jrDataSource);
// number of copies
int numCopies = 1; //hardcode to 1
for(int i = 0; i< numCopies; i++) {
subjectPrintList.add( jasperPrint9 );
}
//choose exporter according to the format the user picked
String reportFormat = "PDF"; //hardcode to PDF
JRExporter exporter = new JRPdfExporter();
String contentType = "application/pdf";
String fileExtension = "pdf";
if ("DOC".equals(reportFormat)) {
exporter = new JRRtfExporter();
contentType = "applicaetion/msword";
fileExtension = "doc";
}
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, subjectPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, pdfOutput);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, new Boolean(true));
exporter.exportReport();
byte[] results = pdfOutput.toByteArray();
ServletOutputStream outstream = response.getOutputStream();
response.setContentType(contentType);
response.setContentLength(results.length);
response.setHeader("Content-disposition","inline; filename=\"Report." + fileExtension + "\"");
outstream.write(results);