package com.hq.jasperreport.servlet;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
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.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.FileBufferedOutputStream;
import net.sf.jasperreports.engine.util.JRLoader;
import com.hq.jasperreport.daomain.Person;
/**
*
* @author 何小虎
* @function 完成报表打印Servlet
*
*/
public class PersonReportServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
ServletContext servletContext = this.getServletConfig()
.getServletContext();
File reportFile = new File(
servletContext.getRealPath("/jasper/personTest.jasper"));
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("Test", "中国移动通信");
// //给jasperreport传递图片对象
// parameters.put("image", servletContext.getRealPath("") +
// "/image/有梦好甜蜜.jpg");
JasperPrint jasperPrint = null;
try
{
JasperReport personJasperReport = (JasperReport) JRLoader
.loadObject(reportFile.getPath());
// 这里是给Person对象赋值
Person p = new Person();
p.setId(1);
p.setName("张三");
p.setGender("男");
p.setAge(12);
p.setAddress("黑龙江哈尔滨");
// 这里是给Person对象赋值
Person p1 = new Person();
p1.setId(2);
p1.setName("李四");
p1.setGender("女");
p1.setAge(20);
p1.setAddress("北京中关村");
// 这里是给Person对象赋值
Person p2 = new Person();
p2.setId(3);
p2.setName("何小虎");
p2.setGender("男");
p2.setAddress("北京中关村软件园");
p2.setAge(20);
// 将Person对象, 放入集合中,
List<Person> personList = new ArrayList<Person>();
personList.add(p);
personList.add(p1);
personList.add(p2);
// 将集合对象数据填充到JasperReport中.
jasperPrint = JasperFillManager.fillReport(personJasperReport,
parameters, new JRBeanCollectionDataSource(personList));
}
catch (JRException e)
{
e.printStackTrace();
}
if (null != jasperPrint)
{
FileBufferedOutputStream fbos = new FileBufferedOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
try
{
exporter.exportReport();
fbos.close();
if (fbos.size() > 0)
{
resp.setContentType("application/pdf");
resp.setContentLength(fbos.size());
ServletOutputStream ouputStream = resp.getOutputStream();
try
{
fbos.writeData(ouputStream);
fbos.dispose();
ouputStream.flush();
}
finally
{
if (null != ouputStream)
{
ouputStream.close();
}
}
}
}
catch (JRException e1)
{
e1.printStackTrace();
}
finally
{
if (null != fbos)
{
fbos.close();
fbos.dispose();
}
}
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
this.doGet(req, resp);
}
}
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
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.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.FileBufferedOutputStream;
import net.sf.jasperreports.engine.util.JRLoader;
import com.hq.jasperreport.daomain.Person;
/**
*
* @author 何小虎
* @function 完成报表打印Servlet
*
*/
public class PersonReportServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
ServletContext servletContext = this.getServletConfig()
.getServletContext();
File reportFile = new File(
servletContext.getRealPath("/jasper/personTest.jasper"));
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("Test", "中国移动通信");
// //给jasperreport传递图片对象
// parameters.put("image", servletContext.getRealPath("") +
// "/image/有梦好甜蜜.jpg");
JasperPrint jasperPrint = null;
try
{
JasperReport personJasperReport = (JasperReport) JRLoader
.loadObject(reportFile.getPath());
// 这里是给Person对象赋值
Person p = new Person();
p.setId(1);
p.setName("张三");
p.setGender("男");
p.setAge(12);
p.setAddress("黑龙江哈尔滨");
// 这里是给Person对象赋值
Person p1 = new Person();
p1.setId(2);
p1.setName("李四");
p1.setGender("女");
p1.setAge(20);
p1.setAddress("北京中关村");
// 这里是给Person对象赋值
Person p2 = new Person();
p2.setId(3);
p2.setName("何小虎");
p2.setGender("男");
p2.setAddress("北京中关村软件园");
p2.setAge(20);
// 将Person对象, 放入集合中,
List<Person> personList = new ArrayList<Person>();
personList.add(p);
personList.add(p1);
personList.add(p2);
// 将集合对象数据填充到JasperReport中.
jasperPrint = JasperFillManager.fillReport(personJasperReport,
parameters, new JRBeanCollectionDataSource(personList));
}
catch (JRException e)
{
e.printStackTrace();
}
if (null != jasperPrint)
{
FileBufferedOutputStream fbos = new FileBufferedOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
try
{
exporter.exportReport();
fbos.close();
if (fbos.size() > 0)
{
resp.setContentType("application/pdf");
resp.setContentLength(fbos.size());
ServletOutputStream ouputStream = resp.getOutputStream();
try
{
fbos.writeData(ouputStream);
fbos.dispose();
ouputStream.flush();
}
finally
{
if (null != ouputStream)
{
ouputStream.close();
}
}
}
}
catch (JRException e1)
{
e1.printStackTrace();
}
finally
{
if (null != fbos)
{
fbos.close();
fbos.dispose();
}
}
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
this.doGet(req, resp);
}
}