jasperreports在web上的报表展现方式有两种,一种是applet viewer,另外一种是flash viewer,由于前者的实现需要浏览器端装有java运行时,所以只考虑后一种方式
<%@ page language="java" contentType="text/html;charset=UTF-8"%> <%@ page import="java.util.*" %>
JasperReports Flash Viewer - Web Application Sample Xml4SwfServlet net.sf.jasperreports.j2ee.servlets.Xml4SwfServlet Xml4SwfServlet /servlets/xml4swf SwfServlet com.easyway.jaserreport.flash.JasperReportFlashServlet SwfServlet /servlets/swf.html index.html BASIC
import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRRuntimeException; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet; import net.sf.jasperreports.j2ee.servlets.Xml4SwfServlet; /** * *请求查看报表时候查询数据,JasperReportFlashServlet用于填充Flash提供 SWF使用表的数据
* * 创建日期 2013-4-28
* @author longgangbai
* @version $Revision$ 2013-4-28 * @since 3.0.0 */ public class JasperReportFlashServlet extends Xml4SwfServlet { // private static final long serialVersionUID = 1L; public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ServletContext context = this.getServletConfig().getServletContext(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); try { String reportFileName = context.getRealPath("reports/WebappReport.jasper"); File reportFile = new File(reportFileName); if (!reportFile.exists()) { throw new JRRuntimeException( "File WebappReport.jasper not found. The report design must be compiled first."); } Mapparameters = new HashMap (); parameters.put("ReportTitle", "Address Report"); parameters.put("BaseDir", reportFile.getParentFile()); JasperPrint jasperPrint = JasperFillManager.fillReport(reportFileName, parameters, new JasperReportDataSource()); request.getSession().setAttribute(request.getSession().getId()+"_"+BaseHttpServlet.JASPER_PRINT_REQUEST_PARAMETER, jasperPrint); request.getRequestDispatcher("/jasperReportSWF.jsp").forward(request, response); } catch (JRException e) { out.println(""); out.println(""); out.println(" JasperReports - Web Application Sample "); out.println(""); out.println(""); out.println(""); out.println("JasperReports encountered this error :"); out.println(""); e.printStackTrace(out); out.println(""); out.println(""); out.println(""); } } }