Solved

Exception in Jasper Report while integrating of Jasper report with Struts

Posted on 2009-05-15
5
892 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

734 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