public void service(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
ServletContext context = this.getServletConfig().getServletContext();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try
{
File reportFile = new File(context.getRealPath("/reports/WebappReport.jasper"));
if (!reportFile.exists())
throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
Map parameters = new HashMap();
parameters.put("ReportTitle", "Address Report");
parameters.put("BaseDir", reportFile.getParentFile());
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
new WebappDataSource()
);
JRHtmlExporter exporter = new JRHtmlExporter();
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image=");
exporter.exportReport();
}
catch (JRException e)
{
out.println("< html >");
out.println("< head >");
out.println("< title >JasperReports - Web Application Sample< /title >");
out.println("< link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\" >");
out.println("< /head >");
out.println("< body bgcolor=\"white\" >");
out.println("< span class=\"bnew\" >JasperReports encountered this error :< /span >");
out.println("< pre >");
e.printStackTrace(out);
out.println("< /pre >");
out.println("< /body >");
out.println("< /html >");
}
}
|