ITPub博客

首页 > 应用开发 > IT综合 > Displaying JasperReports PDF Reports on the Browser

Displaying JasperReports PDF Reports on the Browser

原创 IT综合 作者:yeahokay 时间:2007-08-01 11:12:07 0 删除 编辑

This article provides a brief howto on how to display PDF reports generated with JasperReports from a web application to the user's browser. Basic knowledge of JasperReports and Java Servlet programming is assumed. See the Resources section for links to JasperReports and Servlet programming tutorials and documentation.

The trick to sending a PDF report generated by JasperReports to the user's browser is to call the net.sf.jasperreports.engine.JasperRunManager.runReportToPdf() method. That method has several overloaded versions, the one we will use here has three parameters, a String representing the absolute path of the compiled report (jasper file), an instance of a class implementing the java.util.Map interface, and an instance of a class implementing the net.sf.jasperreports.engine.JRDataSource interface. The JasperRunManager.runReportToPdf() method returns an array of bytes that can be passed as a parameter to the write() method of the javax.servlet.ServletOutputStream class. An instance of ServletOutputStream can be obtained from the getOutputStream() method of the javax.servlet.http.HttpServletResponse class. The best way to explain and visualize all of this is by example, the following code segment demonstrates this technique:

[@more@]

package net.ensode.jasperreportsbrowserdemo;

import java.io.File;
import java.io.IOException;
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 JasperReportsBrowserDemoServlet extends HttpServlet
{

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ServletOutputStream servletOutputStream = response.getOutputStream();
File reportFile = new File(getServletConfig().getServletContext()
.getRealPath("/reports/Simple_Report.jasper"));
byte[] bytes = null;

try
{
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),
new HashMap(), new JREmptyDataSource());

response.setContentType("application/pdf");
response.setContentLength(bytes.length);

servletOutputStream.write(bytes, 0, bytes.length);
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());
}
}
}

As can be seen in the example, the easiest way to obtain the absolute path of the jasper file is to call the getRealPath() method of an instance of a class implementing the javax.servlet.ServletContext interface. For our simple example, we pass an empty instance java.util.HashMap and an instance of net.sf.jasperreports.engine.JREmptyDataSource as the other two parameters to the JasperRunManager.runReportToPdf() method, more complex applications would pass some data inside these two parameters.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/786540/viewspace-929865/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    140
  • 访问量
    1115938