wood burning stoves
The moose likes Beginning Java and the fly likes JasperReports + JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JasperReports + JSP" Watch "JasperReports + JSP" New topic

JasperReports + JSP

Asit Patadia

Joined: Mar 08, 2005
Posts: 7
Hi Ranchers,

I have to generate some reports from JSP using JasperReports. Its a reporting tool from open source community. For testing I have taken a code from a post of Manish Kkum. In that code he tried to generate excel reports.
I m using PostgreSQL db, made some modification in that code. Please find the code as follows:

<%@ page import="java.sql.*, java.util.*, java.lang.*,java.io.* "%>
<%@ page import="net.sf.jasperreports.engine.design.JasperDesign" %>
<%@ page import="net.sf.jasperreports.engine.JasperManager" %>
<%@ page import="net.sf.jasperreports.engine.JRException" %>
<%@ page import="net.sf.jasperreports.engine.JasperReport" %>
<%@ page import="net.sf.jasperreports.engine.JasperPrint" %>
<%@ page import="net.sf.jasperreports.engine.export.JRXlsExporter" %>
<%@ page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter" %>
<%@ page import="net.sf.jasperreports.engine.JRExporterParameter" %>

application.getRealPath("/WEB-INF/lib/jasperreports-0.6.1.jar") +
System.getProperty("path.separator") +
System.setProperty("jasper.reports.compile.temp", application.getRealPath("/reports/"));

ByteArrayOutputStream baos = new ByteArrayOutputStream();

JasperDesign jasperDesign = JasperManager.loadXmlDesign("C:/Tomcat 4.1/webapps/jaspersample/Reports/stud.jrxml");
JasperReport jasperReport = JasperManager.compileReport(jasperDesign);

Map parameters = new HashMap();
Connection conn = getConnection();

JasperPrint jasperPrint = JasperManager.fillReport(jasperReport,parameters, conn);

File destFile = new File("C:/Tomcat 4.1/webapps/jaspersample/Reports", jasperPrint.getName() + ".xls");

JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

byte[] bytes = null;
bytes = baos.toByteArray();
System.out.println("testing: " + bytes.length);


ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
} catch(Exception e) {e.printStackTrace();}


But its not working & giving me following HTTP 500 error:

HTTP Status 500 -

type Exception report


description The server encountered an internal error () that prevented it from fulfilling this request.


org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: -1 in the jsp file: null

Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] C:\Tomcat 4.1\work\Standalone\localhost\jaspersample\jsp\Excel_jsp.java:75: cannot resolve symbol
[javac] symbol : method getConnection ()
[javac] location: class org.apache.jsp.Excel_jsp
[javac] Connection conn = getConnection();
[javac] ^
[javac] 1 error

at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:130)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:293)
at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:353)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:370)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:473)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:190)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:534)

Apache Tomcat/4.1.24

Please Help Me Out.

Asit Patadia.
Aztec Software.
Annie Smith
Ranch Hand

Joined: Mar 05, 2005
Posts: 172
The getConnection() method is not found in your JSP. I suppose it is available in some other class. You should be calling the method on the object reference of the class (or the class name itself, if it is static!)

Cheers!<br /><b>Annie</b>
Asit Patadia

Joined: Mar 08, 2005
Posts: 7
Hi Annie & all ranchers,

Thanx. But I want some more help from you. I am a novice of JasperReports.
Can you suggest me what should be there in getConnection() method; I have just copied that code from a rancher's post. Any help'll be greatly appreciated.

Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
That code doesn't belong in a JSP. JSPs aren't meant for stuff like that, servlets are.

Make it a practice to not put scriptlets in JSPs.

Asit Patadia

Joined: Mar 08, 2005
Posts: 7
Thanx but I am not getting how to run the code. I'll make that code scalable by putting all the java code in servlets & call appropriate JSP afterwords, but first of all I want to know what is the missing thing in this code. Need help from JR(JasperReports) experts.

Thanx & Regards,
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Well, there's a very nice compiler error there. That should tell you what you need to know, your call to getConnection() is calling a nonexistent method.
Sounds like either a typo or a case of RTFM.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

The getConnection() method is not a function of JasperReports, but is rather a function of JDBC -- connecting Java to a database.

What should be in the getConnection() method is what is necessary to obtain a connection to the database. Generally, it's code that looks somewhat like this:

Of course, you would have to replace all parameters with the appropriate values -- consult the database that you are using's documentation for what those might be.

And never mind what Jeroen says -- sometime, having code like this in JSPs makes the most sense, especially for people just tooling around or trying to come up with proof-of-concept. Just keep in mind that for performance and maintainability reasons, you may eventually have to refector this code out into a Servlet or -- more likely -- some sort of utility class available to both servlets and JSPs.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
This is a JSP problem (the error has nothing to do with JasperReports at all), so I'm moving it to the JSP forum...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link: http://aspose.com/file-tools
subject: JasperReports + JSP
It's not a secret anymore!