iReport不显示pdf文件

时间:2013-09-20 19:02:57

标签: servlets pdf jasper-reports

我正在尝试显示我的iReport生成的pdf文件,但我只收到以下错误页面:

“HTTP状态404 -

输入状态报告

消息

description请求的资源()不可用。

Apache Tomcat / 7.0.27“

以下是代码:

package containerweb.reports;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap; 
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import java.math.BigDecimal;


public class AutorizacaoTransportadores extends HttpServlet{
public static Connection con;
public static String banco = "DESENV"; // Nome do banco de dados
public static String usuario = "SCV20"; // Usuario do banco
public static String senha = "SCV20"; // Senha

protected void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    conexao(); 


    String pathJasper = getServletContext().getRealPath(
            "/WEB-INF/relatorios/") + "/";
    String path = getServletContext().getRealPath("/");

    Map parametros = new HashMap();

    parametros.put("codEmpresa", new BigDecimal(26));
    try {
        // Aqui ele cria o relatório
        JasperPrint impressao = JasperFillManager.fillReport(pathJasper
                + "AutorizacaoTransportadores.jasper", parametros ,con);
        // Grava o relatório em disco em pdf
        JasperExportManager.exportReportToPdfFile(impressao, path
                + "/AutorizacaoTransportadores.pdf");
        // Redireciona para o pdf gerado
        res.sendRedirect("AutorizacaoTransportadores.pdf");
    } catch (JRException | IOException e) {
        res.getWriter().println("Erro ao gerar o relatório: " + e);
    }
}

// Cria a conexão
public void conexao() {
    try {
        if (con == null || con.isClosed()) {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(                        
                    "jdbc:oracle:thin:@10.0.0.200:1521:DESENV" /*+ banco*/, usuario, senha);
            System.out.println("Conexão bem sucedida");
        }
    } catch (SQLException | ClassNotFoundException e) {
        System.out.println("não foi possível conectar ao banco ->");
        e.printStackTrace();
    }
}

@Override
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
        throws ServletException, IOException {
    doPost(arg0, arg1);
}

}

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" si:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>containerweb.listener.MyHttpSessionListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>


<!-- <welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>  -->
<error-page>
    <location>/error.html</location>
</error-page>

<servlet>
    <servlet-name>ReportServlet</servlet-name>
    <servlet-class>containerweb.reports.AutorizacaoTransportadores</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ReportServlet</servlet-name>
    <url-pattern>/AutorizacaoTransportadores</url-pattern>
</servlet-mapping>

我在其他项目中使用基本相同的方法有其他报告,并且它们都在工作。我不知道这个可能出现什么问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

在pdf中显示错误此文档可能无法正确显示**

Map<Long, Employee> lookupMap = new HashMap<>(list2.size());
for (Employee e : list2)
    lookupMap.put(e.id, e);

List<Employee> sortedList = new ArrayList<>(list2.size());
for (Long id : list1)
    sortedList.add(lookupMap.get(id));