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

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
主站蜘蛛池模板: 肥乡县| 沾益县| 瑞金市| 龙川县| 兖州市| 贞丰县| 绍兴县| 台山市| 双峰县| 黄梅县| 和林格尔县| 靖江市| 阜康市| 蕲春县| 威宁| 同仁县| 旺苍县| 肥乡县| 勃利县| 页游| 政和县| 渭南市| 合作市| 丹东市| 南澳县| 临泉县| 荥经县| 金秀| 高安市| 南岸区| 大港区| 高雄市| 北京市| 高台县| 安康市| 衡水市| 太仆寺旗| 平陆县| 承德市| 高雄市| 夏津县|