Java Reference
In-Depth Information
That's it! We have successfully created our first report.
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());
 
Search WWH ::




Custom Search