我正在尝试显示我的 iReport 生成的 pdf 文件,但我只收到以下错误页面:
“HTTP 状态 404 -
类型状态报告
信息
描述 请求的资源 () 不可用。
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>
我在其他项目中有其他使用基本相同方法的报告,它们都在工作。我不知道只有这一个可能有什么问题。
提前致谢。