Container를 이용해서 pool객체를 이용하기 위해서는 JNDI를 이용해야 한다.
JNDI는 J2EE의 javax.naming.* 패키지안의 JNDI 클래스를 import해야한다.
============== 데이터를 처리하는 JDBC 로직을 수행하는 파일 ==============
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
.
.
.
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/MySQLDataSource");
con = ds.getConnection();
.
.
.
%>
그리고 프로젝트 WEB-INF\web.xml과 %CATALINA_HOME%\conf\server.xml 을 수정해야 한다.
================ web.xml ============================
<resource-ref>element를 추가한다.
=====================================================
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
.
.
.
<resource-ref>
<res-ref-name>jdbc/MySQLDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
.
.
.
</web-app>
================ server.xml ============================
<Resource>element를 두 곳에 추가한다.
=======================================================
<Server port="8005" shutdown="SHUTDOWN">
.
.
.
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
<Resource auth="Container" name="jdbc/MySQLDataSource" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" password="1234" maxIdle="2" maxWait="5000" username="jspuser"
url="jdbc:mysql://localhost:3306/testboarddb"
maxActive="4"/>
</GlobalNamingResources>
.
.
.
<Service name="Catalina">
.
.
.
<Engine defaultHost="localhost" name="Catalina">
.
.
.
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
.
.
.
<Context docBase="TestBoard" path="/TestBoard" reloadable="true" source="org.eclipse.jst.jee.server:TtestBoard">
<Resource auth="Container" name="jdbc/MySQLDataSource" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" password="1234" maxIdle="2" maxWait="5000" username="jspuser"
url="jdbc:mysql://localhost:3306/testboarddb"
maxActive="4"/>
</Context>
</Host>
</Engine>
</Service>
</Server>
JNDI는 J2EE의 javax.naming.* 패키지안의 JNDI 클래스를 import해야한다.
============== 데이터를 처리하는 JDBC 로직을 수행하는 파일 ==============
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
.
.
.
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/MySQLDataSource");
con = ds.getConnection();
.
.
.
%>
그리고 프로젝트 WEB-INF\web.xml과 %CATALINA_HOME%\conf\server.xml 을 수정해야 한다.
================ web.xml ============================
<resource-ref>element를 추가한다.
=====================================================
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
.
.
.
<resource-ref>
<res-ref-name>jdbc/MySQLDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
.
.
.
</web-app>
================ server.xml ============================
<Resource>element를 두 곳에 추가한다.
=======================================================
<Server port="8005" shutdown="SHUTDOWN">
.
.
.
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
<Resource auth="Container" name="jdbc/MySQLDataSource" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" password="1234" maxIdle="2" maxWait="5000" username="jspuser"
url="jdbc:mysql://localhost:3306/testboarddb"
maxActive="4"/>
</GlobalNamingResources>
.
.
.
<Service name="Catalina">
.
.
.
<Engine defaultHost="localhost" name="Catalina">
.
.
.
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
.
.
.
<Context docBase="TestBoard" path="/TestBoard" reloadable="true" source="org.eclipse.jst.jee.server:TtestBoard">
<Resource auth="Container" name="jdbc/MySQLDataSource" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" password="1234" maxIdle="2" maxWait="5000" username="jspuser"
url="jdbc:mysql://localhost:3306/testboarddb"
maxActive="4"/>
</Context>
</Host>
</Engine>
</Service>
</Server>



