怎么在java web 中通过action调用ireport-CSDN论坛
展开全部
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.bitsperu.academia.reporte;
import com.bitsperu.academia.utilidades.DAO.DAO;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
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.*;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
/**
*
* @author carlos
*/
public class Matricula extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/pdf");
ServletOutputStream out = response.getOutputStream();
Connection cn = DAO.getConexion();
try {
JasperReport reporte = (JasperReport) JRLoader.loadObject(getServletContext().getRealPath("app/matricula/matricula_byid.jasper"));
Map parametros = new HashMap();
parametros.put("id", request.getParameter("id"));
//parametros.put("ot_entidad_id", request.getSession().getAttribute("ot_entidad_id"));
JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, cn);
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.exportReport();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询