Java Reference
In-Depth Information
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
public class HtmlReportServlet extends HttpServlet
{
public static final String REPORT_DIRECTORY = "/reports";
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
ServletContext context =
this.getServletConfig().getServletContext();
String reportName = request.getParameter("reportName");
File file = new File(context.getRealPath(REPORT_DIRECTORY + "/"
+ reportName + ".jrprint"));
PrintWriter printWriter = response.getWriter();
try
{
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file
.getPath());
JRHtmlExporter htmlExporter = new JRHtmlExporter();
response.setContentType("text/html");
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_
PRINT_SESSION_ATTRIBUTE, jasperPrint);
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER,
printWriter);
htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
"image?image=");
htmlExporter.exportReport();
}
catch (JRException e)
{
// display stack trace in the browser
e.printStackTrace(printWriter);
}
}
}
 
Search WWH ::




Custom Search