Supun Silva
Supun Silva

Reputation: 590

JasperReport viewer JSP WebApp error


I am using;

  1. Tomcat 8
  2. Jasper iReport 5.6 also tried Jasper Studio 6.1
  3. Tomcat/lib/jasperreports-6.1.1
  4. Tomcat/lib/mysql-connector-java-5.1.34-bin
  5. Tomcat/lib/commons-beanutils-1.9.0.jar
  6. Tomcat/lib/commons-collections-3.2.1.jar
  7. Tomcat/lib/commons-digester-2.1.jar
  8. Tomcat/lib/commons-logging-1.1.1.jar
  9. Tomcat/lib/groovy-all-2.4.3.jar
  10. itext-2.1.7.js3.jar

I have tried to display report in web server;

<%@ page  import="java.io.*"%> 
<%@ page  import="java.sql.Connection"%> 
<%@ page  import="java.sql.DriverManager"%>
<%@ page  import="java.util.HashMap"%>
<%@ page  import="java.util.Map"%>
<%@ page  import="net.sf.jasperreports.engine.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h2>Hello World!</h2>
    <%
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "root");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        File reportFile = new File(application.getRealPath("report.jasper"));
        Map parameters = new HashMap();
        byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);
        ServletOutputStream outStream = response.getOutputStream();
        outStream.write(bytes, 0, bytes.length);
        outStream.flush();
        outStream.close();
    %>
</body>
</html>

It gives me an attached error on page(jsp and report file in same folder)See the error screenshot

Any idea and suggestion highly appriciated.
I have followed according to this tutorial

Thank you,
Supun

Upvotes: 2

Views: 1059

Answers (2)

Petter Friberg
Petter Friberg

Reputation: 21710

You should really consider deploying your application correctly see the answer of @Ghayel

Your problem is that application.getRealPath("report.jasper") return null, hence it can not find the report.jasper..

Since you insist on working code:

replace

 File reportFile = new File(application.getRealPath("report.jasper"));

with

File reportFile = new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 8.0\\webapps\\reports\\report.jasper");

Make sure that the complied version of your .jrxml the .jasper is there... and the nullpointer will disappear.

Upvotes: 1

Ghayel
Ghayel

Reputation: 1113

Please always create your project directory in $TOMCAT_HOME/webapps/your-project-directory-root/ then insert all your jar files my making one in your project root as WEB-INF/lib

In your-project-directory-root you can add html, css, js, jsp etc and add java files to your /WEB-INF/classes folder

In your case:

  1. Create your project directory in webapps
  2. Add your all images, js, css, html, jsp etc. in your project root directory
  3. Add your all jar files in /WEB-INF/lib folder
  4. also add /WEB-INF/web.xml (deployment descriptor)
  5. Restart tomcat

Cheers!!

Upvotes: 0

Related Questions