Solved

Exception in Jasper Report while integrating of Jasper report with Struts

Posted on 2009-05-15
5
893 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through setting up a JasperReports Server using the AWS EC2 instance, and this article will cover some basic administration tasks I had to perform.
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

688 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