Hi Guys,
I have seen many applications in which reporting functionality is required.
To generate Jasper Reports from Web Application, there is some different logic.
To generate reports through Web Applications, you need to write the report content in response of your server.
Here, I am going to show you only mandatory snippet to generate a PDF report using Web Application.
Add following JARs in your project build path.
Create a servlet to handle your request for report.
Here is the code of servlet.(only doPost() method is given here)
...
...
...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
JasperPrint jasperPrint=null;
try {
String reportFileName="<REPORT_FILE_NAME>";
String reportPath="<REPORT_FILE_PATH>"+reportFileName;
String targetFileName=reportFileName.replace(".jrxml", ".pdf");
final JasperReport jasperReport = JasperCompileManager.compileReport(reportPath);
jasperPrint = JasperFillManager.fillReport(jasperReport,null,JDBCConnection.getJDBCConnection());
ServletOutputStream outputstream = response.getOutputStream();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream);
response.setContentType("application/pdf");
outputstream.write(byteArrayOutputStream.toByteArray());
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-Disposition", "attachment; filename=" + targetFileName);
// clear the output stream.
outputstream.flush();
outputstream.close();
} catch (final JRException e) {
e.printStackTrace();
}
}
I have seen many applications in which reporting functionality is required.
To generate Jasper Reports from Web Application, there is some different logic.
To generate reports through Web Applications, you need to write the report content in response of your server.
Here, I am going to show you only mandatory snippet to generate a PDF report using Web Application.
Add following JARs in your project build path.
- commons-beanutils-1.8.0.jar
- commons-collections-3.1.jar
- commons-digester-2.0.jar
- commons-logging-1.1.1.jar
- groovy-all-1.5.5.jar
- iText-2.1.7.jar
- jasperreports-4.0.1.jar
- mysql-connector-java-5.1.14-bin.jar
Create a servlet to handle your request for report.
Here is the code of servlet.(only doPost() method is given here)
...
...
...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
JasperPrint jasperPrint=null;
try {
String reportFileName="<REPORT_FILE_NAME>";
String reportPath="<REPORT_FILE_PATH>"+reportFileName;
String targetFileName=reportFileName.replace(".jrxml", ".pdf");
final JasperReport jasperReport = JasperCompileManager.compileReport(reportPath);
jasperPrint = JasperFillManager.fillReport(jasperReport,null,JDBCConnection.getJDBCConnection());
ServletOutputStream outputstream = response.getOutputStream();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream);
response.setContentType("application/pdf");
outputstream.write(byteArrayOutputStream.toByteArray());
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-Disposition", "attachment; filename=" + targetFileName);
// clear the output stream.
outputstream.flush();
outputstream.close();
} catch (final JRException e) {
e.printStackTrace();
}
}
And JDBCConnection is the class, where I have write code to make database connection. Here is the code of JDBCConnnection.java
import java.sql.Connection;
import java.sql.DriverManager;
public final class JDBCConnection
{
public static Connection getJDBCConnection()
{
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
}
catch(Exception e)
{
e.printStackTrace();
}
return connection;
}
}
Call this servlet on simple button click.
When you click on button, you will find that the generated report is asked for downloading by your browser.