0% found this document useful (0 votes)
161 views

Flush The Jasper Report Using Java

The document describes a Jasper.java class that is used to generate and stream PDF reports from JasperReports. The class retrieves report parameters, runs the report, and streams the output to the response. Instructions are provided on setting up the project structure, configuring the web.xml file, and deploying the necessary JAR files to generate and serve JasperReports from a servlet.

Uploaded by

krishkarthikn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views

Flush The Jasper Report Using Java

The document describes a Jasper.java class that is used to generate and stream PDF reports from JasperReports. The class retrieves report parameters, runs the report, and streams the output to the response. Instructions are provided on setting up the project structure, configuring the web.xml file, and deploying the necessary JAR files to generate and serve JasperReports from a servlet.

Uploaded by

krishkarthikn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Jasper.java ============ package com.bcs.

report; import import import import import import import import import import import java.io.File; java.io.IOException; java.io.PrintWriter; java.io.StringWriter; java.sql.Connection; java.util.HashMap; javax.servlet.ServletException; javax.servlet.ServletOutputStream; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse;

import com.bcs.db.DBConnection; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperRunManager; public class Jasper extends HttpServlet { /** * */ //private static final long serialVersionUID = 1L; Connection conn=DBConnection.getJNDIConnection(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream servletOutputStream = response.getOutputStream(); File reportFile = new File(getServletConfig().getServletContext().getRealPat h("/report/Load In.jasper")); byte[] bytes = null; try { HashMap<String, Object> jasperParameter = new HashMap<String, Object>(); jasperParameter.put("DOCID","LIS 000002"); jasperParameter.put("PROJECTID","MM ELECTRICALS"); bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), jasperParameter , conn); response.setContentType("application/pdf"); response.setContentLength(bytes.length); servletOutputStream.write(bytes, 0, bytes.length); servletOutputStream.flush(); servletOutputStream.close(); } catch (JRException e)

{ // display stack trace in the browser StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); response.setContentType("text/plain"); response.getOutputStream().print(stringWriter.toString()); } } } ===================================== web.xml ======== <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>Test App</description> <servlet> <servlet-name>LoadIn</servlet-name> <servlet-class>com.bcs.report.LoadIn</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoadIn</servlet-name> <url-pattern>/report/LoadIn.do</url-pattern> </servlet-mapping> <resource-ref> <description>Oracle Datasource example</description> <res-ref-name>jdbc/myoracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app> ================================================= Note ======= 1. Put your Jasper Report files under Report folder. 2. create web.xml and save it in WEB-INF Folder. 3. deploy the jasper file and database connectivity file ,move the class file to classes folder in WEBINF 4. In WEBINF Folder , now we have classes folder - jasper and database class file. web.xml file. lib folder - put jar files, In below mentioned.

commons.collections-3.2.1 commons-beanutils-1.8.2 commons-digester-1.7 commons-logging-1.1 groovy-all-1.7.5 itext-2.1.7(1) jasperreports-4.5.0 ojdbc-6 servlet-api ecj-3.6.2 jasper jasper-el jsp-api 5. give url to flush the jasper report.

You might also like