Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing using ireport 3.7.4 and jsp

 
v.s sandilya
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,

Iam trying to call a ireport using struts by passing a parameter .I have written a code as defined in the attachment.But its not working.
Iam unable to sort out . I prepared using eclipse europa 3.3.0 and ireport 3.7.4 .Kindly help.
with regards,
sandilya.

The code as below


GenerateReport.java


package mypackage;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.HashMap;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import java.math.*;
@SuppressWarnings({"unchecked","deprecation"})
public class GenerateReport {
//private JRResultSetDataSource jrrs=null;
private byte[] bytes=null;
private ServletOutputStream sos=null;
private HttpServletResponse resp=null;
public String OutputFileName=null;
public GenerateReport(HttpServletResponse resp) {
this.resp=resp;
}
public void generatePdf(String rptPath, HashMap hmp,ResultSet rs){
try {

JRResultSetDataSource jrrs=new JRResultSetDataSource(rs);
bytes = JasperRunManager.runReportToPdf(rptPath,hmp,jrrs);
sos=resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment; filename="+OutputFileName+".pdf");
sos.write(bytes);
sos.flush();
sos.close();
} catch (Exception e){
e.printStackTrace();
System.err.println("generatePdf-1 error : "+e.getMessage());
}
}
public void generatePdf(String rptPath, HashMap hmp, Connection con){
try {
bytes=JasperRunManager.runReportToPdf(rptPath, hmp, con);
sos=resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment; filename="+OutputFileName+".pdf");
sos.write(bytes);
sos.flush();
sos.close();
} catch (Exception e){
System.err.println("generatePdf-2 error : "+e.getMessage());
}
}
public void generatePdf(String rptPath, HashMap hmp){
try {
JREmptyDataSource emptRs =new JREmptyDataSource();
bytes = JasperRunManager.runReportToPdf(rptPath,hmp,emptRs);
sos=resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment; filename="+OutputFileName+".pdf");
sos.write(bytes);
sos.flush();
sos.close();
} catch (Exception e){
System.err.println("generatePdf-3 error : "+e.getMessage());
}
}
public void generatePdf(String rptPath, HashMap hashmap,String xmlFileName,String recordPath){
try {
JRXmlDataSource jrxmlds = new JRXmlDataSource(xmlFileName,recordPath);
JasperPrint print = JasperFillManager.fillReport(rptPath, hashmap, jrxmlds);
bytes=JasperManager.printReportToPdf(print);
sos=resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment; filename="+OutputFileName+".pdf");
sos.write(bytes);
sos.flush();
sos.close();
} catch (Exception e) {
System.err.println("generatePdf-4 error : "+e.getMessage());
}
}
public void generatePdf(String rptPath, HashMap hashmap,StringBuffer xml,String recordPath){
try {
DocumentBuilderFactory fac=DocumentBuilderFactory.newInstance();
DocumentBuilder db =fac.newDocumentBuilder();
InputSource insrc=new InputSource();
insrc.setCharacterStream(new StringReader(xml.toString()));
Document doc=db.parse(insrc);
JRXmlDataSource jrxmlds = new JRXmlDataSource(doc,recordPath);
JasperPrint print = JasperFillManager.fillReport(rptPath, hashmap, jrxmlds);
bytes=JasperManager.printReportToPdf(print);
sos=resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment; filename="+OutputFileName+".pdf");
sos.write(bytes);
sos.flush();
sos.close();
} catch (Exception e) {
System.err.println("generatePdf-5 error : "+e.getMessage());
}
}
public void generatePdfForJrxml(String rptPath, HashMap hashmap,String xmlFileName,String recordPath){
try {
JRXmlDataSource jrxmlds = new JRXmlDataSource(xmlFileName,recordPath);
JasperReport rpt=JasperCompileManager.compileReport(rptPath);
JasperPrint print = JasperFillManager.fillReport(rpt, hashmap, jrxmlds);
bytes=JasperManager.printReportToPdf(print);
sos=resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment; filename="+OutputFileName+".pdf");
sos.write(bytes);
sos.flush();
sos.close();
} catch (Exception e) {
System.err.println("generatePdf-6 error : "+e.getMessage());
}
}
public void generateXls(String rptPath, HashMap hashmap,ResultSet rs){
try {
JRResultSetDataSource jrds = new JRResultSetDataSource(rs);
JasperPrint print = JasperFillManager.fillReport(rptPath, hashmap, jrds);
sos=resp.getOutputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, baos);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE , Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.exportReport();
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition", "attachment; filename="+OutputFileName+".xls");
sos.write(baos.toByteArray());
sos.flush();
sos.close();
} catch (Exception e){
System.err.println("generatePdf-7 error : "+e.getMessage());
}
}

reportaction.java

package mypackage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class reportAction extends Action {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String query;
String target="failure";
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception,IOException {
reportForm formBean =(reportForm)form;
String p_employee_id = formBean.getEmployee_id();
System.out.print(p_employee_id);
try{
target="success";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:shanu";
con = DriverManager.getConnection(url, "", "");
stmt=con.createStatement();
String query="select employee_id,employee_name,hours_worked from employee where employee_id=" + p_employee_id ;
rs=stmt.executeQuery(query);

String rpt=(String)request.getRealPath("/iReport")+"/"+"employee_report.jasper";
HashMap<String,String> parameterMap = new HashMap<String,String>();
parameterMap.put("employee_id",p_employee_id );
GenerateReport gp=new GenerateReport(response);
gp.OutputFileName="emp list";
gp.generatePdf(rpt, parameterMap,rs);
return null;
}catch (Exception e){
e.printStackTrace();
System.err.println(e.getMessage());
}
finally{
try {
if(stmt!=null)stmt.close();
}catch (Exception e) {
System.err.println("Exception== "+e.getMessage());
}
}
return mapping.findForward(target);
}



}

reportform.java
package mypackage;

public class reportForm extends org.apache.struts.action.ActionForm{
private String employee_id;
public void setEmployee_id(String employee_id)
{
this.employee_id =employee_id;
}
public String getEmployee_id()
{
return employee_id;
}
}



Search_employees.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function search_employee()
{
alert ('hii');
document.forms[0].action="report_generate.do";
document.forms[0].submit();
alert ('hii there');
}

</script>
</head>

<body>
<form action="" method="post" name="form1" >
ENTER EMPLOYEE ID TO FETCH <input name="employee_id" id="employee_id" type="text" />
<input type="button" name="search" id="search" value="Search" / onclick="search_employee()">
</form>
</body>
</html>


}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic