-1

I am trying to integrate my report in JSP page. Report created using jaspersoft studio. But i am getting null pointer exception on runReportToPdf line. I am totally new to this report and web application. So please someone help to resolve this and get excepted result. Quick support will be very much appreciated. Thanks.

<%@ 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 trimDirectiveWhitespaces="true" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Report!</h2>
<%
            Connection con = null;

   try{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.248:1521:incomingqc", "inqc", "megaWIN123$");
              System.out.println("i AM IN CONNECTION");
            }catch (Exception ex) {
             System.out.println("i AM ex para");
                ex.printStackTrace();
            }
            
            File reportFile = new File(application.getRealPath("Blank_A4_Landscape.jasper"));//your report_name.jasper file
            Map parameters = new HashMap();
            byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, con);
 
            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);
            
            ServletOutputStream outStream = response.getOutputStream();
            outStream.write(bytes, 0, bytes.length);
            outStream.flush();
            outStream.close();
        %>
    </body>
</html>
Alex K
  • 22,315
  • 19
  • 108
  • 236
kavi
  • 163
  • 1
  • 9

1 Answers1

0

PDF and HTML content cannot be mixed. When the JSF page generates the HTML, it writes to a temporary buffer. That temporary buffer is sent to the browser once JSF is told that the response is complete.

In the given code, here is a rough idea of what the web browser will receive:

<html><head></head><body><h2>Report!</h2>%PDFNΘs​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s...</body></html>

For the browser to display the report there are a few options:

  • Generate an HTML version of the report and use <h:outputFormat> to inject the report content into the existing web page.
  • Generate a PDF version of the report and use <h:commandLink> or <h:commandButton> with an action that writes the PDF to the browser, much as you've already done.

Mixing both approaches as shown in the question, however, will not produce the desired result.

See also:

Community
  • 1
  • 1
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315