Reputation: 590
I am using;
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)
Any idea and suggestion highly appriciated.
I have followed according to this tutorial
Thank you,
Supun
Upvotes: 2
Views: 1059
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
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:
Cheers!!
Upvotes: 0