官术网_书友最值得收藏!

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:

Displaying Reports on a Web Browser
主站蜘蛛池模板: 海城市| 江安县| 若羌县| 东宁县| 安徽省| 香河县| 博湖县| 绵竹市| 自贡市| 岳阳市| 蒙阴县| 武定县| 通许县| 碌曲县| 裕民县| 泰和县| 大姚县| 沙田区| 奉新县| 元谋县| 治多县| 通河县| 越西县| 淮安市| 临泉县| 益阳市| 德阳市| 黄龙县| 新乡县| 资源县| 千阳县| 齐齐哈尔市| 来凤县| 乳源| 乌拉特中旗| 东山县| 江西省| 界首市| 台南市| 教育| 会泽县|