Mapping the JasperReports ImageServlet
The JasperReports framework includes a servlet named ImageServlet for displaying images in generated HTML reports. Even if you do not include images in your report, you must add a mapping for this servlet, because JasperReports uses blank images for spacing.
Complete the following steps to map the image URI to ImageServlet. You use the image URI later when you add code to export the report to HTML.
1. In the Files window expand TravelReport, expand web, expand WEB-INF, and double-click the
web.xml node to open it in the XML editor. 2. In the editing toolbar, click Servlets to open the Servlet Editor. 3. In the Servlets editor, click Add Servlet Element.
4. The Add Servlet dialog box appears as shown in the following figure.
Figure 6: Add Servlet Dialog Box
5. In the Add Servlet dialog box, type the following values and click OK.
Text Box Servlet Name Servlet Class URL Patterns
Value ImageServlet net.sf.jasperreports.j2ee.servlets.ImageServlet /image
6. Close web.xml and save the changes.
7. Now, the application knows that when it receives a request that is addressed to image, the
application should send the request to the ImageServlet instance.
Adding the Report Generation Logic
Follow these steps to add logic to the application bean for generating the report.
1. In the Projects window, double-click the Application Bean node to open ApplicationBean1.java
in the editing area. 2. Add the following code to the end of the file before the final end brace.
Code Sample 4: Report Generation Code /**
*
Prefix to the resource name for compiled reports.
*/private static final String PREFIX = \
/**
*
Suffix to the resource name for compiled reports.
*/private static final String SUFFIX = \
/**
*
Valid content types for reports that we can produce.
*/private static final String[] VALID_TYPES =
{ \ // Standard HTML representation \ // Adobe Portable Document Format };
/**
*
Generate the specified report, in the specified output * format, based on the specified data.
** @param name Report name to be rendered
* @param type Content type of the requested report (\ * @param data ResultSet containing the data to report * @exception IllegalArgumentException if the specified * content type is not recognized
* @exception IllegalArgumentException if no compiled report definition * for the specified name can be found */
public void jasperReport(String name, String type, ResultSet data) {
jasperReport(name, type, data, new HashMap()); }
/**
*
Generate the specified report, in the specified output * format, based on the specified data.
*
* @param name Report name to be rendered
* @param type Content type of the requested report (\ * or \
* @param data ResultSet containing the data to report * @param params Map of additional report parameters *
* @exception IllegalArgumentException if the specified * content type is not recognized
* @exception IllegalArgumentException if no compiled report definition * for the specified name can be found
or \ */
public void jasperReport(String name, String type, ResultSet data, Map params) {
// Validate that we recognize the report type // before potentially wasting time filling the // report with data boolean found = false;
for (int i = 0; i < VALID_TYPES.length; i++) { if (VALID_TYPES[i].equals(type)) { found = true; break; } }
if (!found) {
throw new IllegalArgumentException(\ + \ }
// Look up the compiled report design resource ExternalContext econtext = getExternalContext();
InputStream stream = econtext.getResourceAsStream(PREFIX + name + SUFFIX); if (stream == null) {
throw new IllegalArgumentException(\ + \ }
// make sure cursor is in front of the first record try {
data.beforeFirst(); } catch (Exception e) {
throw new FacesException(e); }
// Fill the requested report with the specified data
JRResultSetDataSource ds = new JRResultSetDataSource(data); JasperPrint jasperPrint = null; try {
jasperPrint = JasperFillManager.fillReport(stream, params, ds); } catch (RuntimeException e) { throw e;
} catch (Exception e) {
throw new FacesException(e); } finally { try {
stream.close(); } catch (IOException e) { ; } }
// Configure the exporter to be used, along with the custom // parameters specific to the exporter type JRExporter exporter = null;
HttpServletResponse response = (HttpServletResponse) econtext.getResponse();
FacesContext fcontext = FacesContext.getCurrentInstance(); try {
response.setContentType(type); if (\ exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); } else if (\ exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter());
// Make images available for the HTML output HttpServletRequest request = (HttpServletRequest)
fcontext.getExternalContext().getRequest(); request.getSession().setAttribute(
ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); exporter.setParameter(
JRHtmlExporterParameter.IMAGES_MAP, new HashMap()); // The following statement requires mapping /image // to the imageServlet in the web.xml. //
// This servlet serves up images including the px // images for spacing. //
// Serve up the images directly so we
// don't incur the extra overhead associated with // with a JSF request for a non-JSF entity.
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + \ }
} catch (RuntimeException e) { throw e;
} catch (Exception e) {
throw new FacesException(e); }
// Enough with the preliminaries ... export the report already try {
exporter.exportReport(); } catch (RuntimeException e) { throw e;
} catch (Exception e) {
throw new FacesException(e); }
// Tell JavaServer Faces that no output is required fcontext.responseComplete(); }