1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| public class JasperServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("[JasperServlet] ");
try {
String format = null;
String jasperFilename = null;
String database = null;
JasperReport jasperReport = null;
// paramètres à envoyer au rapport
Map<String, Object> parameters = new HashMap<String, Object>();
Enumeration<?> en = request.getParameterNames();
while (en.hasMoreElements()) {
String parameterName = (String) en.nextElement();
if (parameterName.equals("format")) {
format = request.getParameter(parameterName);
} else if (parameterName.equals("jasperFilename")) {
jasperFilename = request.getParameter(parameterName);
} else if (parameterName.equals("jdbcURI")) {
database = request.getParameter("jdbcURI");
if (database != null) {
if (database.length() == 0) {
database = null;
}
}
} else {
if (request.getParameter(parameterName).equals("null")) {
parameters.put(parameterName, "%");
} else {
parameters.put(parameterName, request.getParameter(parameterName));
}
log.info("[JasperServlet] [Param:" + parameterName + "]=" + request.getParameter(parameterName));
}
}
String realPath = getServletContext().getRealPath("/");
jasperFilename = realPath + jasperFilename;
log.info("[JasperServlet] jasperFilename=" + jasperFilename);
if (jasperFilename.toLowerCase().endsWith(".jrxml")) {
JasperDesign jasperDesign = null;
try {
FileInputStream in = new FileInputStream(jasperFilename);
jasperDesign = JRXmlLoader.load(in);
} catch (Exception e) {
e.printStackTrace();
}
if (jasperDesign == null) {
log.error("jasperDesign load failled");
return;
}
jasperReport = JasperCompileManager.compileReport(jasperDesign);
}
Database db = null;
if (database == null) {
db = new Database(log, databaseUrl, databaseName, databaseLogin, databasePassword, logDebug, false);
} else {
db = new Database(log, database, logDebug, false);
}
if (db.sqlConnect()) {
Connection connection = db.getConnection();
JasperPrint jasperPrint = null;
if (jasperReport == null) {
log.info("[JasperServlet] jasperFilename=" + jasperFilename);
FileInputStream in = new FileInputStream(jasperFilename);
jasperPrint = JasperFillManager.fillReport(in, parameters, connection);
} else {
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
}
JRExporter exporter = getExporter(format, response);
ServletOutputStream outStream = response.getOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outStream);
exporter.exportReport();
outStream.flush();
outStream.close();
} else {
ServletOutputStream outStream = response.getOutputStream();
PrintWriter pw = new PrintWriter(outStream);
pw.write("<html>");
pw.write("<h1>SQL Connect problem:</h1>");
pw.write("<hr />");
pw.write(db.getErrorMessage());
pw.write("</html>");
pw.flush();
pw.close();
outStream.flush();
outStream.close();
}
log.info("[JasperServlet] job done");
} catch (Exception e) {
e.printStackTrace();
log.error("[JasperServlet] exception:" + e.toString());
ServletOutputStream outStream = response.getOutputStream();
PrintWriter pw = new PrintWriter(outStream);
pw.write("<html>");
pw.write("<h1>" + e.toString() + "</h1>");
pw.write("<hr />");
e.printStackTrace(pw);
pw.write("</html>");
pw.flush();
pw.close();
outStream.flush();
outStream.close();
}
}
public JRExporter getExporter(String format, HttpServletResponse response) {
JRExporter exporter = null;
if (format.equalsIgnoreCase("html")) {
exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, "./images/jasper_tmp/");
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "images/jasper_tmp/");
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_FILE_NAME, "report.html");
response.setHeader("Content-Disposition", "inline;filename=rapport.html");
response.setContentType("text/html");
} else if (format.equalsIgnoreCase("xls")) {
exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_FILE_NAME, "report.xls");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline;filename=rapport.xls");
} else if (format.equalsIgnoreCase("pdf")) {
exporter = new JRPdfExporter();
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_FILE_NAME, "report.pdf");
response.setHeader("Content-Disposition", "inline;filename=rapport.pdf");
response.setContentType("application/pdf");
}
return exporter;
}
} |
Partager