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.
Hi,Using website templates allows setting up websites of any type. Whether you want to create a personal website or business website, website templates can make this task not only easier for Web Design Cochin but also time- and budget-saving. Website templates designs include HTML templates, PSD templates, Flash templates. To choose unique web template, you will need help of experienced web designers.Thanks..............
ReplyDeleteHi Arpana,
DeleteIt's not about website template. It is the dynamic report generation using iReport Tool with Jasper Reports API which is basically used to generate report in either PDF, Excel ,etc formats. Drop an email to dhavalce1989@gmail.com if you want to know more about it.
Regards,
Dhaval Shah
Great Article
ReplyDeleteOnline Java Training | Java Training in Chennai | Java 360