- JasperReports for Java Developers
- David R. Heffelfinger
- 462字
- 2021-04-29 19:07:31
Displaying Reports on a Web Browser
In the previous section, we discussed how to create a report and save it to disk in JasperReports' native format. In this section, we will explain how to display a report in a web browser with the help of the Servlet API. The following example demonstrates how to accomplish this:
package net.ensode.jasperbook;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
public class FirstReportSendToBrowserServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletOutputStream servletOutputStream = response.getOutputStream();
InputStream reportStream = getServletConfig().getServletContext()
.getResourceAsStream("/reports/FirstReport.jasper");
try
{
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), new JREmptyDataSource());
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
}
catch (JRException e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
}
Since web browsers are incapable of displaying reports in JasperReports' native format (at least without the help of an applet), we must export the report to a format that the browser can understand. JasperReports allows us to export reports to PDF and many other formats. Since the PDF format is widely used, we chose to export to this format in this example.
The servlet in the above example calls the static JasperRunManager.runReportToPdfStream()
method. The signature for this method is:
runReportToPdfStream(java.io.InputStream inputStream, java.io.OutputStream outputStream, java.util.Map parameters, JRDataSource dataSource)
To display the report on the browser, we need to pass the binary report template, or Jasper file, in the form of a stream, as the first argument of this method. We can accomplish this by calling the javax.servlet.ServletContext.getResourceAsStream()
method, passing a String
containing the location of the Jasper file as a parameter. This method will return an instance of java.io.InputStream
that we can use as the first argument for the JasperRunManager.runReportToPDFStream()
method.
The JasperRunManager.runReportToPDFStream()
method needs an instance of java.io.OutputStream()
to write the compiled report. We can simply use the default output stream for the servlet, which can be obtained by calling the javax.servlet.http.HttpServletResponse.getOutputStream()
method.
The next two arguments for the JasperRunManager.runReportToPDFStream()
method are java.util.Map
and JRDataSource
. The former is used to pass any parameters to the report, the latter to pass data in the form of a net.sf.jasperreports.engine.JRDataSource
. Since we are not passing any parameters or data to this simple report, an empty HashMap
and JREmptyDataSource
suffice.
To make sure the browser displays the report properly, we must set the content type to application/pdf
. We can accomplish this by calling the javax.servlet.http.HttpServletResponse.setContentType()
method.
The resulting code for this example needs to be deployed to a servlet container. An ANT script to automate this process can be found at this book's website. The following screenshot shows the report being displayed as a PDF on a browser:

- MATLAB計算機視覺經典應用
- PS職場達人煉成記:人人都能學會的Photoshop辦公設計技巧
- AI圖像處理:Photoshop+Firefly后期處理技術基礎與實戰
- UG NX 9.0中文版 基礎教程 (UG工程師成才之路)
- Rhino 6.0中文版入門、精通與實戰
- Dreamweaver CC實例教程(第5版·微課版)
- PowerPoint 2016實戰從入門到精通(超值版)
- AutoCAD 2016中文版基礎教程(全圖解視頻版)
- 邊做邊學:Illustrator CS6平面設計案例教程(微課版)
- 計算機輔助翻譯基礎與實訓
- Excel 2010 Financials Cookbook
- Revit技巧精選應用教程
- Photoshop 2020實戰從入門到精通(超值版)
- 從零開始:Dreamweaver CS6中文版基礎培訓教程
- HBase企業應用開發實戰