Jasper Report Calling from JSF Page
Step-1. Create ReportBean.Java file as a bean class
/****** ReportBean.java Class Start*******************/
package com.report;
public class ReportBean {
private Date startDate;
private Date endDate;
private String customerID=null;
private String customerName;
private String itemcode;
private String itemname;
ReportDao dao=new ReportDao();
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
LoginSession ses=(LoginSession)session.getAttribute("SESSION");
/******* PDF Report ***********/
public void reportCustomer(){
try {
String filename = "Report/KITR009.jasper";
FacesContext facesContext = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
//file path of .jasper file
String path = servletContext.getRealPath("/");
String filePath=path+File.separator+filename;
String imgPath=path+File.separator+"images"+File.separator+"MedisysLogo.gif";
dao.report0009(ses.getUserid(),imgPath,filePath);
} catch (Exception e) {
}
}
/******* HTML Report ***********/
public void reportSalesMan(){
try {
String filename = "Report/KITR0042.jasper";
FacesContext facesContext = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
//file path of .jasper file
String path = servletContext.getRealPath("/");
String filePath=path+File.separator+filename;
String imgPath=path+File.separator+"images"+File.separator+"MedisysLogo.gif";
dao.report0042(ses.getUserid(),imgPath,filePath);
} catch (Exception e) {
}
}
//////////////////// GETTER SETTER ///////////////////////
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
System.out.println("startDate:: "+startDate);
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerID(String customerID) {
this.customerID = customerID;
}
public String getCustomerID() {
return customerID;
}
}
/****** ReportBean.java Class END*******************/
/**************** ReportDao call form bean ***********/
/********* Start ReportDao.java *********/
package com.sa.medisys.banamco.report;
public class ReportDao extends DBConnection{
/* Customer Information PDF Report */
public void report0009(String userid,String imgPath,String filePath ) throws SQLException, JRException, IOException, NamingException {
try{
System.out.println("User ID ::"+userid);
Map
parameter = new HashMap();
parameter.put("USER_ID", userid);
parameter.put("LOGO_PATH", imgPath);
Connection con=dbOpen();
System.out.println("Connection Established"+ con);
JasperPrint jasperPrint =JasperFillManager.fillReport(filePath, parameter,con);
//System.out.println("Report Created..."+parameter.get("itemtype"));
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response =(HttpServletResponse) context.getExternalContext().getResponse();
byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
System.out.println("bytes >> "+bytes.length);
response.setHeader("Content-disposition", "attachment; filename=\"KITR0009.pdf\"");
//response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);
response.setContentType("application/pdf");
//response.setContentType("text/html; charset=utf-8");
//response.setHeader ("Content-disposition", "attachment; filename=\"CustomerReport.pdf\"");
context.responseComplete();
dbClose(con);
//session.removeAttribute("kIT0053Bean");
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
/* Salesman Information HTML Report */
public void report0042(String userid,String imgPath,String filePath ) throws SQLException, JRException, IOException, NamingException{
Connection con=dbOpen();
FacesContext context = FacesContext.getCurrentInstance();
try{
System.out.println("User ID ::"+userid);
Map parameter = new HashMap();
parameter.put("USER_ID", userid);
parameter.put("LOGO_PATH", imgPath);
System.out.println("Connection Established"+ con);
JasperPrint jasperPrint =JasperFillManager.fillReport(filePath, parameter,con);
// html ////////
System.out.println("Report Created... in Format");
JRExporter exporter = null;
HttpServletResponse response =(HttpServletResponse) context.getExternalContext().getResponse();
response.setContentType("text/html; charset=utf-8");
response.setHeader ("Content-disposition", "attachment; filename=\"KITR0042.html\"");
exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, response.getWriter());
exporter.exportReport();
context.responseComplete();
}catch (Exception e) {
System.out.println(e.getMessage());
}finally{
dbClose(con);
System.out.println("Created >>>>>>>>");
}
}
}
/********* END ReportDao.java *********/