JasperReport B/S Web应用

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
  
/**
 *   
 * <p>Web方式请求简单的JasperReport Servlet  <p>
 *
 * 创建日期 2013-4-22<br>
 * @author  longgangbai<br>
 * @version $Revision$ 2013-4-22
 * @since   3.0.0
 */
public class JasperReportServlet extends HttpServlet {  
   
 private static final String CONTENTTYPE = "application/octet-stream";   
  
    /** 
     * Constructor of the object. 
     */  
    public JasperReportServlet() {  
        super();  
    }  
  
    /** 
     * Destruction of the servlet. <br> 
     */  
    public void destroy() {  
        super.destroy(); // Just puts "destroy" string in log   
        // Put your code here   
    }  
    private static final long serialVersionUID = -2610486659053659764L;
    public JRDataSource createReportDataSource() {
        JRDataSource dataSource;
        Collection<Map<String, ?>> reportRows = initializeMapArray();
        dataSource = new JRMapCollectionDataSource(reportRows);
        return dataSource;
    }
    public Collection<Map<String, ?>> initializeMapArray() {
        Collection<Map<String, ?>> reportRows = new ArrayList<Map<String, ?>>();
        HashMap<String, Object> row1Map = new HashMap<String, Object>();
        HashMap<String, Object> row2Map = new HashMap<String, Object>();
        HashMap<String, Object> row3Map = new HashMap<String, Object>();
        HashMap<String, Object> row4Map = new HashMap<String, Object>();
        row1Map.put("tail_num", "N263Y");
        row1Map.put("aircraft_serial", "T-11");
        row1Map.put("aircraft_model", "39 ROSCOE TRNR RACER");
        row1Map.put("engine_model", "R1830 SERIES");
        row2Map.put("tail_num", "N4087X");
        row2Map.put("aircraft_serial", "BA100-163");
        row2Map.put("aircraft_model", "BRADLEY AEROBAT");
        row2Map.put("engine_model", "R2800 SERIES");
        row3Map.put("tail_num", "N43JE");
        row3Map.put("aircraft_serial", "HAYABUSA 1");
        row3Map.put("aircraft_model", "NAKAJIMA KI-43 IIIA");
        row3Map.put("engine_model", "R1830 SERIES");
        row4Map.put("tail_num", "N912S");
        row4Map.put("aircraft_serial", "9973CC");
        row4Map.put("aircraft_model", "PA18-150");
        row4Map.put("engine_model", "R-1820 SER");
        reportRows.add(row1Map);
        reportRows.add(row2Map);
        reportRows.add(row3Map);
        reportRows.add( row4Map);
        return reportRows;
    }
    /** 
     * The doGet method of the servlet. <br> 
     * 
     * This method is called when a form has its tag value method equals to get. 
     *  
     * @param request the request send by the client to the server 
     * @param response the response send by the server to the client 
     * @throws ServletException if an error occurred 
     * @throws IOException if an error occurred 
     */  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
		try {
		    response.setCharacterEncoding("utf-8");
	        response.setContentType("application/json;charset=utf-8");
	        response.setHeader("pragma", "no-cache");
	        response.setHeader("cache-control", "no-cache");
	        response.setContentType("text/html;charset=utf-8");
	        PrintWriter pw = response.getWriter();
	        // 创建数据源
	        JRDataSource jrdatasource = createReportDataSource();
	        String reportFile = getServletConfig().getServletContext().getRealPath("/resources/data/AircraftReport.jrxml");
	          
	        
	        JasperReport jrt = JasperCompileManager.compileReport(reportFile);//编译报表格式 
	           //填充报表数据生成JasperPrint对象 
	        JasperPrint jasperPrint = JasperFillManager.fillReport(jrt, new HashMap<String, Object>(), jrdatasource);
	        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, pw);
	        // 设置不显示jasperreports报表中的线条
	        // 显示线条的设置的参数
	        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, true);
	        //制定图片请求的路径
	        exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/images/px.");
	        exporter.exportReport();
	        pw.flush();
	        pw.close();
		} catch (JRException e) {
			e.printStackTrace();
		}  
    }  
       
  
      
  
} 

 

在web请求JasperReport报表缺少图片

解决方案如下:

      在WebRoot目录添加images/px文件,px文件来源jasperreports-5.0.4\src\net\sf\jasperreports\engine\images目录下pixel.GIF修改为px.px

      在Servlet添加请求设置图片路径的代码:

// 显示线条的设置的参数
	        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, true);
	        //制定图片请求的路径
	        exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/images/px.");
	    

 

 

 

你可能感兴趣的:(打印,导出,jasperReport,展示,Web容器整合)