User Tools

Site Tools


java:jsp

Java Server Pages

JNDI de Oracle + Tomcat

I don't know why, but the docummentation about how to properly set a JNDI oracle entry in Tomcat is not correctly specified. Many documentns scattered here and there, but no one with a full working, modern example.

In this page I will put the basic files and configuration needed to set a test page in Tomcat that loads a simple JNDI with the oracle driver (no connection pooling by now).

Moreover, here is the whole project, with dependencies:

jndi_test.zip

Create a web application under tomcat webapps directory

In this case, I've create a directory called “jndi_test”. Under that directory, create “WEB-INF”, “META-INF” and “WEB-INF\lib”:

And now, put the following contents:

index.jsp

<!doctype html>
<html lang="es">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.naming.Context,
				javax.naming.InitialContext, 
				javax.sql.DataSource, 
				java.sql.Connection,
				java.sql.Statement,
				java.sql.ResultSet,
				oracle.jdbc.driver.OracleDriver" %>
 
<h1>JNDI With Oracle test page</h1>
 
<p>Test that context class exists....</p>
<%
	Context initCtx = new InitialContext();
%>
 
<p>Check that the environment context can be loaded....</p>
<%
	Context envCtx = (Context) initCtx.lookup("java:comp/env");
%>
 
<p>Check if our JNDI exist jdbc/my_simple_oracle_jndi...</p>
<%
	DataSource jndi = (DataSource) envCtx.lookup("jdbc/my_simple_oracle_jndi");
%>
 
<p>Creamos una conexión de base de datos...</p>
<%
// Allocate and use a connection from the pool
Connection conn = jndi.getConnection();
%>
 
<p>Ejecutamos una consulta...</p>
<%
try( Statement cmd = conn.createStatement() ){
	ResultSet rs = cmd.executeQuery( "select count(*) total, systimestamp now from df_cmn_aum_xx_tsk_job" ); 
	rs.next();
	out.println( "<p>" + rs.getLong("total") + "</p>" );
	out.println( "<p>" + rs.getDate("now") + "</p>" );
}
%>
 
<p>Y finalmente cerramos la conexion....</p>
<%
// and finally, close the connection to be a good citizen
conn.close();
%>
</html>

WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
		xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
		id="WebApp_ID" 
		version="4.0">
 
  <display-name>jndi_test</display-name>
 
 
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
	<welcome-file>index.html</welcome-file>
  </welcome-file-list>
 
  <resource-env-ref>
	<description>Pruebas jndi</description>
	<resource-env-ref-name>jdbc/my_simple_oracle_jndi</resource-env-ref-name>
	<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
  </resource-env-ref>
 
 
</web-app>

META-INF\context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
 
 <Resource name="jdbc/my_simple_oracle_jndi" 
		auth="Container" 
		type="javax.sql.DataSource"
        maxActive="100" 
		maxIdle="30" 
		maxWait="10000"
        username="YOUR-ORACLE-USERNAME" 
		password="YOUR-ORACLE-PASSWORD" 
		driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@YOUR-ORACLE-HOST:YOUR-ORACLE-PORT/YOUR-ORACLE-SERVICE-OR-SID"/>
 
</Context>

WEB-INF/lib

Put the contents of the JDBC oracle driver of choice but removing the files xmlparserv2.jar and xmlparserv2_sans_jaxp_services.jar because they collide with the tomcat libs. I've used Oracle 11's. This have been tested in Tomcat 8 and JDK 1.8.

java/jsp.txt · Last modified: 2022/12/02 21:02 by 127.0.0.1