Jump to content
JasperReports Library 7.0 is now available ×

processing report on Servlet


2004 IR Help

Recommended Posts

By: Alan Shiers - ashiers

processing report on Servlet

2005-05-29 12:31

Hi there,

 

I'm stuck! I have a pre-compiled jasper report named "CourseEvalReport.jasper". In my servlet I am able to load the report file to an InputStream object and passing it as a parameter to the JasperFillManager.fillReport(...) method. Now, I need to send the report out as an HttpServletResponse to the client in HTML format. This is where I'm stuck. How do I do that?

Servlet class source code below.

 

Alan

 

 

 

public class CourseEvalReportProcessor extends HttpServlet

{

public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException

{

doPost(req,res);

}

 

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException

{

res.setContentType("text/html");

PrintWriter out = res.getWriter();

 

String CompanyID = req.getParameter("CompanyID");

String CourseID = req.getParameter("CourseID");

String SubmissionDate = req.getParameter("SubmissionDate");

String DaysMissedAverage = "1-3";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

java.util.Date today = new java.util.Date();

String ReportDate = sdf.format(today);

String ReportingServletURL = res.encodeRedirectURL("/scholastic/CourseEvalReportProcessor");

 

//create a map of parameters to pass to the report.

Map<String, String> parameters = new HashMap<String,String>();

parameters.put("CompanyID", CompanyID);

parameters.put("CourseID", CourseID);

parameters.put("SubmissionDate", SubmissionDate);

parameters.put("DaysMissedAverage", DaysMissedAverage);

parameters.put("ReportDate", ReportDate);

parameters.put("ReportingServletURL", ReportingServletURL);

 

try

{

Connection connection = null;

Context ctx = new InitialContext();

if(ctx == null )

throw new Exception("No Context available...");

 

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Scholastic");

 

if (ds != null)

{

connection = ds.getConnection();

if(connection != null)

{

//get the pre-compiled report

InputStream inputStream = mvcs.CourseEvalReportProcessor.class.getResourceAsStream("/jasper/CourseEvalReport.jasper");

 

if(inputStream == null)

{

System.out.println("inputStream is null");

}

else

{

//System.out.println("inputStream is not null");

//create JasperPrint using fillReport() method

JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, connection);

 

//Send the report back out as HttpServletResponse

//The following is not what I really want to do.

//JasperExportManager.exportReportToHtmlFile(jasperPrint, java.lang.String);

 

}

}

}

}

catch(SQLException sqle)

{

System.err.println("SQL Problem: " + sqle.getMessage());

System.err.println("SQL State: " + sqle.getSQLState());

System.err.println("Vendor Error: " + sqle.getErrorCode());

sqle.printStackTrace();

}

catch(Exception e){e.printStackTrace();}

}

 

}

 

 

 

 

By: Alan Shiers - ashiers

RE: processing report on Servlet

2005-05-29 14:34

Hi there,

 

Ok, I finally found the webapp example from the source project files. I followed an example from a file named HTMLServlet.java. I came up with the servlet code below. However, when I run it, I get the following error message from Tomcat:

 

java.lang.NoClassDefFoundError: bsh/EvalError

net.sf.jasperreports.engine.design.JRBshCompiler.loadCalculator(JRBshCompiler.java:95)

net.sf.jasperreports.engine.design.JRDefaultCompiler.loadCalculator(JRDefaultCompiler.java:156)

net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:327)

net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:83)

net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:75)

net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:68)

net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:248)

mvcs.CourseEvalReportProcessor.doPost(CourseEvalReportProcessor.java:82)

mvcs.CourseEvalReportProcessor.doGet(CourseEvalReportProcessor.java:23)

javax.servlet.http.HttpServlet.service(HttpServlet.java:689)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

 

There is a JRBshCompiler class, however, it must be trying to reference some other class named bsh/EvalError which doesn't exist anywhere that I can see.

 

I really need some help on this. Please advise.

 

Alan

 

***********************************************************

 

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException

{

Connection connection = null;

System.out.println("entering doPost");

res.setContentType("text/html");

PrintWriter out = res.getWriter();

 

...

//create a map of parameters to pass to the report.

Map<String, String> parameters = new HashMap<String,String>();

parameters.put("CompanyID", CompanyID);

parameters.put("CourseID", CourseID);

parameters.put("SubmissionDate", SubmissionDate);

parameters.put("DaysMissedAverage", DaysMissedAverage);

parameters.put("ReportDate", ReportDate);

parameters.put("ReportingServletURL", ReportingServletURL);

 

try

{

Context ctx = new InitialContext();

if(ctx == null )

throw new Exception("No Context available...");

 

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Scholastic");

 

if (ds != null)

{

connection = ds.getConnection();

if(connection != null)

{ System.out.println("got connection");

//get the pre-compiled report

ServletContext context = this.getServletConfig().getServletContext();

 

InputStream inputStream = mvcs.CourseEvalReportProcessor.class.getResourceAsStream("/jasper/CourseEvalReport.jasper");

 

if(inputStream == null)

{

System.out.println("inputStream is null");

}

else

{

System.out.println("creating report...");

JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);

System.out.println("got jasper report...");

//create JasperPrint using fillReport() method

 

//THIS NEXT LINE KEEPS THROWING AN EXCEPTION!!

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);

JRHtmlExporter exporter = new JRHtmlExporter();

 

//Send the report back out using PrintWriter object

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);

//exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);

//exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image=");

System.out.println("exporting report...");

exporter.exportReport();

 

}

}

}

}

...

...

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...