Java Reference
In-Depth Information
PrintWriter printWriter = response.getWriter();
InputStream reportStream = getServletConfig().getServletContext()
.getResourceAsStream("/" + REPORT_DIRECTORY + "/" + reportName
+ ".jasper");
JasperPrint jasperPrint;
try
{
Class.forName("com.mysql.jdbc.Driver"); connection =
DriverManager.getConnection("jdbc:mysql://localhost:
3306/flightstats?user=root&password=password");
jasperPrint = JasperFillManager.fillReport(reportStream,
new HashMap(), connection);
JRHtmlExporter htmlExporter = new JRHtmlExporter();
response.setContentType("text/html");
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_
PRINT_SESSION_ATTRIBUTE, jasperPrint);
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER,
printWriter);
htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
"image?image=");
htmlExporter.exportReport();
connection.close();
}
catch (Throwable t)
{
// display stack trace in the browser
t.printStackTrace(printWriter);
}
}
}
The main difference between this example and the previous one is that instead
of loading the JasperPrint object from the file system, we are loading the Jasper
template, filling it, and storing the filled report in a JasperPrint object. This instance
is then used as the value for the JRExporterParameter.JASPER_PRINT parameter.
 
Search WWH ::




Custom Search