JasperReport + Servlet Web应用之:HTML显示报表内容
构造我们的JavaBean:(get,set方法省略,属性名称要与报表的字段名)
public class Person {private String person_Id;private String person_name;private String person_age;private String person_address;}
?构造我们的Service层:
public class PersonService {public List<Person> getAllPerson() {List<Person> perList = new ArrayList<Person>();perList.add(new Person("101", "小博", "22", "湖北"));perList.add(new Person("102", "张三", "21", "湖南"));perList.add(new Person("103", "李四", "23", "江苏"));perList.add(new Person("104", "王五", "22", "上海"));perList.add(new Person("101", "小博", "22", "湖北"));return perList;}}
?构造我们的Servlet:
public class JRHTMLServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {ServletContext context = this.getServletConfig().getServletContext();File jasperFile = new File(context.getRealPath("/jasper/preson.jasper"));Map<String, String> parameters = new HashMap<String, String>();parameters.put("year", "2009");parameters.put("unit_mc", "武汉XX科技有限公司");JasperPrint jasperPrint = null;response.setCharacterEncoding("utf-8");try {JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperFile);List<Person> personList = new PersonService().getAllPerson();jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,new JRBeanCollectionDataSource(personList));} catch (JRException e) {e.printStackTrace();}if(null != jasperPrint){JRHtmlExporter exporter = new JRHtmlExporter();request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter());exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"image?image=");try {exporter.exportReport();} catch (JRException e) {e.printStackTrace();}}}}
?web.xml配置:
<servlet><servlet-name>JRHTMLServlet</servlet-name><servlet-class>com.mengya.servlet.JRHTMLServlet</servlet-class></servlet><servlet-mapping><servlet-name>JRHTMLServlet</servlet-name><url-pattern>/JRHTMLServlet</url-pattern></servlet-mapping>
?在web.xml还要配置JasperReport的自带的一个Serlvet,不然显示报表内空时样式不好看。
<!-- JasperReport包中自带的Servlet,用来做为HTML形式显示报表的时候报表的图片,要在这里配置上,图片在jasperReport包中 --><servlet><servlet-name>JasperReportImageServlet</servlet-name><servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class></servlet><servlet-mapping><servlet-name>JasperReportImageServlet</servlet-name><url-pattern>/image</url-pattern></servlet-mapping>
?这里的servlet路径要与上面我写的那个serlvet中的JRHtmlExporterParameter.IMAGES_URI值一样!
??页面调用:
<a href="JRHTMLServlet">Servlet中生成HTML</a>
?
不通过servlet直接通过jsp出生的方式如下:
<a href="jsp/viewer.jsp">JSP中生成报表显示</a>
?
viewer.jsp内空如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@ page import="com.mengya.JRDataSource.PersonDataSource " %><%@ page import="net.sf.jasperreports.engine.*" %><%@ page import="net.sf.jasperreports.engine.util.*" %><%@ page import="net.sf.jasperreports.engine.export.*" %><%@ page import="net.sf.jasperreports.j2ee.servlets.*" %><%@ page import="java.io.*" %><%@ page import="com.mengya.bean.Person" %><%@ page import="com.mengya.service.PersonService" %><%@ page import="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource" %><%JasperPrint jasperPrint = (JasperPrint)session.getAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE);if (request.getParameter("reload") != null || jasperPrint == null){File reportFile = new File(application.getRealPath("/jasper/preson.jasper"));if (!reportFile.exists())throw new JRRuntimeException("File preson.jasper not found. The report design must be compiled first.");JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());Map parameters = new HashMap();parameters.put("year", "2009");parameters.put("unit_mc", "武汉XX科技有限公司");List<Person> personList = new PersonService().getAllPerson();jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(personList));session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);}JRHtmlExporter exporter = new JRHtmlExporter();int pageIndex = 0;int lastPageIndex = 0;if (jasperPrint.getPages() != null){lastPageIndex = jasperPrint.getPages().size() - 1;}String pageStr = request.getParameter("page");try{pageIndex = Integer.parseInt(pageStr);}catch(Exception e){}if (pageIndex < 0){pageIndex = 0;}if (pageIndex > lastPageIndex){pageIndex = lastPageIndex;}StringBuffer sbuffer = new StringBuffer();exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../image?image=");exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(pageIndex));exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");exporter.exportReport();%><html><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><head> <style type="text/css"> a {text-decoration: none} </style></head><body text="#000000" link="#000000" alink="#000000" vlink="#000000"><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr> <td width="50%"> </td> <td align="left"> <hr size="1" color="#000000"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><a href="viewer.jsp?reload=true"><img src="../images/reload.GIF" border="0"></a></td> <td> </td><%if (pageIndex > 0){%> <td><a href="viewer.jsp?page=0"><img src="../images/first.GIF" border="0"></a></td> <td><a href="viewer.jsp?page=<%=pageIndex - 1%>"><img src="../images/previous.GIF" border="0"></a></td><%}else{%> <td><img src="../images/first_grey.GIF" border="0"></td> <td><img src="../images/previous_grey.GIF" border="0"></td><%}if (pageIndex < lastPageIndex){%> <td><a href="viewer.jsp?page=<%=pageIndex + 1%>"><img src="../images/next.GIF" border="0"></a></td> <td><a href="viewer.jsp?page=<%=lastPageIndex%>"><img src="../images/last.GIF" border="0"></a></td><%}else{%> <td><img src="../images/next_grey.GIF" border="0"></td> <td><img src="../images/last_grey.GIF" border="0"></td><%}%> <td width="100%"> </td> </tr> </table> <hr size="1" color="#000000"> </td> <td width="50%"> </td></tr><tr> <td width="50%"> </td> <td align="center"><%=sbuffer.toString()%> </td> <td width="50%"> </td></tr></table></body></html>
??