/* Save as : "jesper.jsp" */
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.*"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="net.sf.jasperreports.engine.design.JasperDesign"%>
<%@ page import="net.sf.jasperreports.engine.xml.JRXmlLoader"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Jesper Report</h2>
<form method="POST" action="jesper.jsp?rpt=1">
<input type="Submit" name="submit" value="Generate Report"/>
</form>
<%
String sign = request.getParameter("rpt");
if(sign==null)
{
sign="0";
}
if(sign.equals("0"))
{out.print("<b>Please click the button and download the report.</b>");
}else{
%>
<%
Calendar calendar = new GregorianCalendar();
String am_pm;
int datetoday = calendar.get(Calendar.DATE);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
%>
<%
Connection conn = null;
String s = String.valueOf(second);
String d = String.valueOf(datetoday);
String m = String.valueOf(month);
String y = String.valueOf(year);
String date_time = String.valueOf(s+d+m+y);
//out.println(date_time);
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/data", "root", "gftsd4");
InputStream input = new FileInputStream(new File("D:/MyInstall/Apache Software Foundation/Tomcat 7.0_JSP/webapps/Jesper_Report_Jsp/EmployeeReport.jrxml"));
JasperDesign jasperDesign = JRXmlLoader.load(input);
out.println("Compiling Report Designs<br/>");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
out.println("Creating JasperPrint Object<br/>");
Map parameters = new HashMap ();
parameters.put("Id", "select First_Name, Last_Name from employee where Id < 100");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);
//Exporting the report
OutputStream output = new FileOutputStream(new File("D://EmployeeReport-'"+date_time+"'.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, output);
out.println("Report Generation Complete");
out.print("<br/><br/>");
out.println("Generated Report name is: "+"<b>EmployeeReport-"+date_time+".pdf</b>");
/* File download code start*/
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment;filename=EmployeeReport-'"+date_time+"'.pdf");
File file = new File("D://EmployeeReport-'"+date_time+"'.pdf");
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out1 = response.getOutputStream();
byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 4096) != -1)
{
out1.write(outputByte, 0, 4096);
}
fileIn.close();
out1.flush();
out1.close();
/* File download code close */
conn.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
//------------------------ EmployeeReport.jrxml ----------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="EmployeeReport_aman.kumar">
<parameter name="Id" class="java.lang.String"/>
<queryString>
<![CDATA[$P!{Id}]]>
</queryString>
<field name="First_Name" />
<field name="Last_Name" />
<title>
<band height="50">
<staticText>
<reportElement x="0" y="0" width="180" height="15"/>
<textElement/>
<text><![CDATA[Jasper Report - By Java]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="50">
<image>
<reportElement x="0" y="0" width="100" height="50"/>
<imageExpression><![CDATA["D://cooltext769884152.png"]]></imageExpression>
</image>
<staticText>
<reportElement x="0" y="0" width="69" height="24" />
<textElement verticalAlignment="Bottom" />
<text>
<![CDATA[First Name: ]]>
</text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="79" height="24" />
<text>
<![CDATA[Last Name: ]]>
</text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="30">
<textField>
<reportElement x="0" y="0" width="69" height="24" />
<textFieldExpression>
<![CDATA[$F{First_Name}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="69" height="24" />
<textFieldExpression>
<![CDATA[$F{Last_Name}]]>
</textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="50">
<textField>
<reportElement x="439" y="22" width="100" height="20" />
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>