Reputation: 1114
There are more then 100 reports in application. I would like to integrate them in large web application.
I found the idea here : http://jasperreports.sourceforge.net/sample.reference/webapp/index.html.
So, I would like to create Servlet which will return pdf, html or xlsx formats of reports. I've already done that by example for one Report class. Moreover, my servlet returns type which it receives from parameter.
However, I can not realize writing (with mapping etc) for each report class. How can I avoid that by using same servlets for different reports (different classes). In this case - MyDataExample.
Here it is :
public class ServletExample extends HttpServlet {
@Override
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
MyDataExample masterData = new MyDataExample(new ReportParameters(Long.valueOf(req.getParameter("id")), 2, DatabaseConnection.getConnection(), null));
JasperPrint print = masterData.build();
req.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);
String outputType = req.getParameter("outputType");
Exporter exporter;
switch (outputType) {
case "html":
try {
PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
req.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);
exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(out);
output.setImageHandler(new WebHtmlResourceHandler("/reports/image?image={0}"));
exporter.setExporterOutput(output);
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
break;
case "pdf":
resp.setContentType("application/pdf");
exporter = new JRPdfExporter(DefaultJasperReportsContext.getInstance());
exporter.setExporterInput(new SimpleExporterInput(print));
try (OutputStream outputStreams = resp.getOutputStream()) {
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStreams));
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
}
break;
case "xlsx":
resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
exporter = new JRXlsxExporter(DefaultJasperReportsContext.getInstance());
exporter.setExporterInput(new SimpleExporterInput(print));
try (OutputStream outputStream = resp.getOutputStream()) {
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(false);
configuration.setWhitePageBackground(false);
exporter.setConfiguration(configuration);
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
break;
default:
throw new RuntimeException("Unknown type selected");
}
}
}
Upvotes: 1
Views: 618
Reputation: 1114
So, I found that everything was easy, just refresh the page because your browser can cache the page. So, you could get different instances by using different values of same parameter, for example:
if(req.getParameter("className").equals("A"))
classObject = new A();
else
classObject = new B();
This example works perfect, also you can automate this actions by creating class example according to its name by using reflection.
Upvotes: 2