Struts 2 and Jasper Report Integration Example

This struts 2 tutorials explain how to integrate Jasper iReport in your dynamic web application to generate the PDF report. Before starting the coding, we need to include Jasper iReport JAR dependency in Struts 2 project.

Software Used

  1. Eclipse
  2. MySQL Database
  3. JDK 8
  4. Tomcat 8
  5. iReport-5.6.0

Required JARS

Struts 2 and Jasper iReport integration required these jar files.

  1. asm-3.3.jar
  2. asm-commons-3.3.jar
  3. asm-tree-3.3.jar
  4. commons-beanutils-1.8.0.jar
  5. commons-collections-3.1.jar
  6. commons-digester-2.0.jar
  7. commons-fileupload-1.2.2.jar
  8. commons-logging-api-1.1.jar
  9. commons-io-2.0.1.jar
  10. commons-lang-2.5.jar
  11. commons-logging-1.1.3.jar
  12. freemarker-2.3.18.jar
  13. jasperreports-5.6.0.jar
  14. jasperreports-javaflow-5.6.0.jar
  15. javassist-3.11.0.GA.jar
  16. ognl-3.0.4.jar
  17. struts2-core-
  18. xwork-core-
  19. mysql-connector-java-5.1.38-bin

Project Structure in Eclipse

Struts 2 and Jasper iReport Integration Example

How to Start Coding

Sometimes it’s a big deal which code wrote first or how to start coding? To get your code error free follow these steps…

Step- 1 : Very first step is create the table script.

CREATE TABLE `pdfreport` (
  `NAME` varchar(20) DEFAULT NULL,
  `EMAIL` varchar(50) DEFAULT NULL,
  `MOBILE` varchar(12) DEFAULT NULL,
  `COLLEGE` varchar(50) DEFAULT NULL,
  `COURSE` varchar(10) DEFAULT NULL,
  `SKILLS` varchar(100) DEFAULT NULL,

Step– 2: Design your report. Show Report Code

Step– 3: Include all required jars in project lib folder.

Step– 4: Add Struts 2 filter in web.xml


Step– 5: Create DAO class.
package org.websparrow;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ReportDAO {

	public ResultSet getPdf() {

		try {
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
			String query = "SELECT NAME, email, mobile, college, course, skills, marks1, marks2, marks3 FROM pdfreport";
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(query);

			return rs;
		} catch (Exception e) {
			return null;



Step– 6: Create Action class.
package org.websparrow;

import java.sql.ResultSet;
import java.util.HashMap;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

public class ReportAction extends ActionSupport {

	public ResultSet rs = null;
	public String submit = null;
	public InputStream fileInputStream;
	public String jasperPath = "";
	public String pdfName = "";
	public String rpt = "";

        //Generate Getters and Setters... 

	public String execute() throws Exception {
		try {
			if (submit.equals("pdf")) {
				rs = new ReportDAO().getPdf();
				jasperPath = ServletActionContext.getServletContext().getRealPath("/Reports");
				pdfName = "Student Report";
				rpt = "studentReport.jrxml";
				JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
				HashMap<String, Object> pm = new HashMap<String, Object>();
				String logo = jasperPath + "/ws.jpg";
				pm.put("logo", logo);
				JasperReport jr = JasperCompileManager.compileReport(jasperPath + "/" + rpt);
				JasperPrint jp = JasperFillManager.fillReport(jr, pm, resultSetDataSource);
				JasperExportManager.exportReportToPdfFile(jp, jasperPath + pdfName + ".pdf");
				fileInputStream = new FileInputStream(new File(jasperPath + pdfName + ".pdf"));

		} catch (Exception e) {
		return "SUCCESS";


Step– 7: Design your User Interface page.

<%@taglib prefix="s" uri="/struts-tags"%>
<style type="text/css">
button {
	background-color: #008CBA;
	border: none;
	color: white;
	padding: 15px 32px;
	text-align: center;
	text-decoration: none;	
	display: inline-block;
	font-size: 16px;
	border-radius: 12px;
<body style="text-align: center;">
	<h1>Struts2 and Jasper iReport Integration Example</h1>
	<form method="POST" name="rpt" action="StudentReport.action">

		<button type="submit" name="submit" value="pdf">Generate Report</button>


Step– 8: Map the Action class in struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<constant name="struts.devMode" value="true" />
	<package name="default" extends="struts-default" namespace="/">
		<action name="StudentReport" class="org.websparrow.ReportAction">
			<result name="SUCCESS" type="stream">
				<param name="contentType">application/octet-stream</param>
				<param name="inputName">fileInputStream</param>
				<param name="contentDisposition">attachment;filename="${pdfName}.pdf"</param>
				<param name="bufferSize">1024</param>

Step- 9: Finally all is done, now start you server and run the project.

Output :

Find the URL to test the application http://localhost:8080/Struts2JasperReportIntegration/. We will get output as given below.

Screen 1 Struts 2 and Jasper iReport Integration Example Screen 2 Struts 2 and Jasper iReport Integration Example Screen 3 Struts 2 and Jasper iReport Integration Example
Share this article on: