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

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
主站蜘蛛池模板: 宝应县| 礼泉县| 阿瓦提县| 玛沁县| 大庆市| 锦屏县| 元江| 晋江市| 扶风县| 建平县| 阳谷县| 平武县| 都兰县| 广河县| 公主岭市| 泉州市| 宁德市| 陆川县| 澄迈县| 河东区| 高陵县| 安顺市| 永川市| 剑阁县| 龙陵县| 灵寿县| 瑞丽市| 项城市| 罗源县| 平顺县| 南澳县| 淮北市| 南皮县| 林州市| 平邑县| 滁州市| 贞丰县| 行唐县| 遂溪县| 从江县| 呼伦贝尔市|