response.setHeader("Content-Type", "application/pdf") renvoie la valeur null
Bonjour,
En fait, je voudrais créer un Schedule task qui ne génère un état pdf. Le premier problème était de retrouvé le chemin de l'état avec getPath() a été résolu, le second problème est comme je l'ai intitulé response.setHeader("Content-Type", "application/pdf"); me renvoi la valeur null.
voici mon code
SendMessage.java
Code:
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
| public class SendMessage implements Job {
private ServletContext servletContext;
private void createPdfRendezvous() {
HttpServletResponse response = (HttpServletResponse) this.servletContext;
Connection connection = null;
@SuppressWarnings("unused")
Statement statement = null;
String pathreport = ServletContextListner.getApplicationCntx().getRealPath("/")
+"report\\rpt_test.jasper";
File filereport = new File(pathreport);
try{
connection = Database_report.getConnection();
statement = connection.createStatement();
@SuppressWarnings("rawtypes")
Map parameters = new HashMap();
@SuppressWarnings("unchecked")
byte[] bytes = JasperRunManager.runReportToPdf(filereport.getPath(),parameters,connection);
response.setHeader("Content-Type", "application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0, bytes.length);
outStream.flush();
outStream.close();
} catch (Exception e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
}
public void execute(JobExecutionContext context) throws JobExecutionException {
createPdfRendezvous();
}
} |
Et le fichier ServletContextListner.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class ServletContextListner implements ServletContextListener {
private static ServletContext servletContext;
@Override
public void contextDestroyed(ServletContextEvent contextEvent) {
servletContext = null;
System.out.println("context destroyed");
}
@Override
public void contextInitialized(ServletContextEvent contextEvent) {
servletContext = contextEvent.getServletContext();
System.out.println("context Initialized");
}
public static ServletContext getApplicationCntx(){
return servletContext;
}
} |
Merci