Estou tentando fazer um relatorio teste, funcionar usando uma pool de conexoes (antes, eu estava utilizando uma conexao direta com o banco). Pois bem, dei uma pesquizada no forum e vi um post explicando +- como se faz isso. Estou tendo alguns problemas, aqui vai o codigo:
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/hibernate</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>reportService</servlet-name>
<servlet-class>report.ReportHibernate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>reportService</servlet-name>
<url-pattern>/reportService</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@page contentType="text/html"%>
<%@page 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>JSP Page</title>
</head>
<body>
<a href="<%= request.getContextPath()%>/reportService">Link</a>
</body>
</html>
public class PoolConnection {
/**
*
* @return a connection from the connection pool
* @throws java.lang.Exception
*/
public Connection getConnection()
throws Exception {
// Buscar contexto JNDI
Context ic = new InitialContext();
// Solicitar o objeto Data Source
DataSource ds = (DataSource) ic.lookup("jdbc/hibernate");
// Obter uma conexao
Connection conn = (Connection) ds.getConnection();
return conn;
}
}
public class ReportHibernate extends HttpServlet {
private static final long serialVersionUID = -433379001619945275L;
private Logger logger = Logger.getLogger("ReportHibernate");
@SuppressWarnings("unchecked")
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException {
try {
PoolConnection pool = new PoolConnection();
Connection con = pool.getConnection();
JasperReport jasperReport = null;
byte[] pdfRelatorio = null;
String sep = File.separator;
String pathJasper = getServletContext().getRealPath("/content/reports/") + sep;
Map parametros = new HashMap();
jasperReport = (JasperReport) JRLoader.loadObject(
pathJasper + "relatorioHibernate.jasper");
pdfRelatorio = JasperRunManager.runReportToPdf(jasperReport, parametros, con);
logger.info("CaminhoJasper: " + pathJasper);
logger.info("JasperReport : " + jasperReport);
logger.info("Parametros : " + parametros);
logger.info("Pdf : " + pdfRelatorio);
//Parametros para nao fazer cache e o que será exibido..
res.setContentType("application/pdf");
res.setHeader("Cache-Control", "no-store");
res.setHeader("Pragma", "no-cache");
res.setDateHeader("Expires", 0);
//Envia para o navegador o pdf..
ServletOutputStream servletOutputStream = res.getOutputStream();
servletOutputStream.write(pdfRelatorio);
servletOutputStream.flush();
servletOutputStream.close();
} catch (Exception ex) {
Logger.getLogger(ReportHibernate.class.getName()).error(ex);
}
}
}
estou tento essa mensagem de erro:
11:30:07,687ERROR ReportHibernate:65 - javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
e entao, o que mais preciso fazer para funcionar? estou usando o tomcat 6.0 com o banco mysql 5.0