Error generating pdf report from a jsp page

Asked

Viewed 386 times

-1

I have a problem generating pdf report using jsp and jasperreport.

Initially, on my desktop where my server was glashfish, the code ran well, but when publishing the page on a Tomcat server, it gave error "error: java.lang.Outofmemoryerror: Metaspace".

So I switched my server from my IDE to Tomcat and now the error is nullpointexception:

type Exception report

message An exception occurred processing JSP page /sys/imprimir-os.jsp at line 131

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

exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /sys/imprimir-os.jsp at line 131

128:                         filtro.put("NUMEROOS", Integer.parseInt(os));
129: 
130:                         try {
131:                             byte[] bytes = JasperRunManager.runReportToPdf(caminho.getRealPath(src), filtro, conn);
132:                             response.setContentType("application/pdf");
133:                             response.setContentLength(bytes.length);
134:                             ServletOutputStream outputStream = response.getOutputStream();


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)


root cause
java.lang.NullPointerException
    java.io.File.<init>(File.java:277)
    net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:281)
    net.sf.jasperreports.engine.JasperRunManager.runToPdf(JasperRunManager.java:254)
    net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:755)
    org.apache.jsp.sys.imprimir_002dos_jsp._jspService(imprimir_002dos_jsp.java:262)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

Below follows the code excerpt.

<form class="form-sys" action="" method="POST">
              <label>Informe o Número da Ordem de Serviço: <input class="ipt-form-sys" type="text" name="numeroos"></label> <input type="submit" value="Imprimir">
                <%
                    String os = request.getParameter("numeroos");
                    if (os != null) {
                        Connection conn = ConnectionFactory.getConnection();

                        String src = "sys/relatorios/protocolo-de-entrada.jasper";
                        ServletContext caminho = request.getServletContext();

                        HashMap filtro = new HashMap();
                        filtro.put("NUMEROOS", Integer.parseInt(os));

                        try {
                            byte[] bytes = JasperRunManager.runReportToPdf(caminho.getRealPath(src), filtro, conn);
                            response.setContentType("application/pdf");
                            response.setContentLength(bytes.length);
                            ServletOutputStream outputStream = response.getOutputStream();
                            outputStream.write(bytes, 0, bytes.length);
                            outputStream.flush();
                            outputStream.close();
                        } catch (JRException e) {
                            e.printStackTrace();
                        } finally {
                            conn.close();
                        }
                    } else {
                    }
                %>
            </form>

I just want to generate the report for printing without downloading and without using Servlet.

  • The error refers to this line: byte[] bytes = Jasperrunmanager.runReportToPdf(path.getRealPath(src), filter, Conn);

1 answer

0

I found a solution and it worked very well... I will leave the code as an example to those who need...

Connection conn = ConnectionFactory.getConnection();
                                            String jasper = null;
                                            jasper = "/sys/relatorios/protocolo-de-entrada.jasper";
                                            File reportFile = new File(application.getRealPath(jasper));
                                            Map parameters = new HashMap();
                                            parameters.put("NUMEROOS", Integer.parseInt(os));
                                            byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);
                                            response.setContentType("application/pdf");
                                            response.setContentLength(bytes.length);
                                            ServletOutputStream ouputStream = response.getOutputStream();
                                            ouputStream.write(bytes, 0, bytes.length);
                                            ouputStream.flush();
                                            ouputStream.close();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.