Solved

Exception in Jasper Report while integrating of Jasper report with Struts

Posted on 2009-05-15
5
889 Views
Last Modified: 2013-11-24
I m getting the null value in         InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream("/reports/DbReport.jasper");
 While i integrate the jasper report with struts framework.

i have already the compile file with DbReport.jasper in report folder
i m attaching the DbReport.jasper file also..

please tell me why this exception and null value come

package net.ensode.jasperbook.struts;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.util.HashMap;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperRunManager;
 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

public class GenerateReportAction extends Action

{

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception

{

    Connection connection=null;

    ServletOutputStream servletOutputStream =response.getOutputStream();

 

    InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream("/reports/DbReport.jasper");

    
 

    System.out.println("ReportStream---------->>>>>>>>>>>>>>>>"+reportStream);

    response.setContentType("application/pdf");
 

     try{

           Class.forName("com.mysql.jdbc.Driver").newInstance();

           connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ashu_db","root","mysql");

           System.out.println("After load Driver========================");

      

           JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream, new HashMap(), connection);

        }catch(Exception e){

            e.printStackTrace();

        }

    finally{

      connection.close();

      servletOutputStream.flush();

      servletOutputStream.close();

    }

    return mapping.getInputForward();

    }

}

Open in new window

0
Comment
Question by:achitaley
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Gibu George
ID: 24394805
InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream(<request.getContextPath()+"/reports/DbReport.jasper");
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24394811
remove the < sign is a typo

InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream(request.getContextPath()+"/reports/DbReport.jasper");
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24395091
Try
InputStream reportStream = new FileInputStream(request.getRealPath("/reports/DbReport.jasper"));

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24400402
make sure your reports directory is inside your classes directory

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24571418
:-)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now