내가알기로는 tomcat 5.5 부터 server.xml 의 context 부분이 분리되어 설정하도록 정책이 바뀐것으로 기억한다.
내기억으로는 권고안정도로 기억하는데… 오늘 관련설정을하다 한참을 삽질을 했으므로… 다음을 위해 기록해둔다.
환경은 아래와 같다.
linux (fedora 14)
jdk 1.6.0_25
tomcat 6
수정할 파일은
tomcat의 server.xml, context.xml 그리고 웹어플리케이션의 web.xml
1. 먼저 server.xml 에서 <GlobalNamingResources> 안쪽에
<Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@192.168.225.130:1521:ora9i" username="scott" password="tiger" maxActive="20" maxIdle="10" maxWait="-1"/>
url="jdbc:oracle:thin:@192.168.225.130:1521:ora9i" username="scott" password="tiger" maxActive="20" maxIdle="10" maxWait="-1"/>
라고 추가해준다.
2. context.xml 에서 <Context> 안쪽에
<ResourceLink global="jdbc/myoracle" name="jdbc/myoracle" type="javax.sql.DataSource"/>
라고 추가
3. web.xml에서
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
jdbc/myoracle 이라는 이름으로 db접속정보들을 들고 다닐것이니… 주황색바탕으로 되어있는 부분이 같도록 설정해준다.
코드상에서 사용법은…
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
StringBuffer sql = new StringBuffer();
sql.append("select count(*) from dept");
int result = 0;
try{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql.toString());
rs.next();
result = rs.getInt(1);
}catch(SQLException se){
se.printStackTrace();
}
%>
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
StringBuffer sql = new StringBuffer();
sql.append("select count(*) from dept");
int result = 0;
try{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql.toString());
rs.next();
result = rs.getInt(1);
}catch(SQLException se){
se.printStackTrace();
}
%>
아래는 풀소스
-- server.xml
닫기
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<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" />
<!-- jndi 설정 -->
<Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@192.168.225.130:1521:ora9i" username="scott" password="tiger" maxActive="20" maxIdle="10" maxWait="-1"/>
<!-- //jndi 설정 -->
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Context docBase="jndiTest" path="/jndiTest" reloadable="true" source="org.eclipse.jst.jee.server:jndiTest" />
</Host>
</Engine>
</Service>
</Server>
<Server port="8005" shutdown="SHUTDOWN">
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<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" />
<!-- jndi 설정 -->
<Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@192.168.225.130:1521:ora9i" username="scott" password="tiger" maxActive="20" maxIdle="10" maxWait="-1"/>
<!-- //jndi 설정 -->
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Context docBase="jndiTest" path="/jndiTest" reloadable="true" source="org.eclipse.jst.jee.server:jndiTest" />
</Host>
</Engine>
</Service>
</Server>
닫기
-- Context.xml
less..
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- jndi 설정 -->
<ResourceLink global="jdbc/myoracle" name="jdbc/myoracle" type="javax.sql.DataSource"/>
<!-- //jndi 설정 -->
</Context>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- jndi 설정 -->
<ResourceLink global="jdbc/myoracle" name="jdbc/myoracle" type="javax.sql.DataSource"/>
<!-- //jndi 설정 -->
</Context>
less..
--web.xml
닫기
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>jndiTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- jndi 설정 -->
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- //jndi 설정 -->
</web-app>
<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">
<display-name>jndiTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- jndi 설정 -->
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- //jndi 설정 -->
</web-app>
[출처] http://javafactory.tistory.com/entry/jndi-%EC%84%A4%EC%A0%95-tomcat-6
'네트워크 > WAS' 카테고리의 다른 글
java.net.BindException: Address already in use: JVM_Bind (0) | 2015.11.20 |
---|---|
톰캣 Document Root 설정 방법 (0) | 2015.11.20 |
[Servlet] 웹 서버의 구동, 종료와 서블릿의 라이프 사이클(Life-Cycle) (0) | 2015.11.20 |
MySQL DB 생성/삭제 및 사용자 추가/삭제, 권한 부여 (0) | 2015.11.06 |
아파치 톰캣서버(Apache Tomcat Server) 포트(Port)번호 변경하기 (톰캣 웹서버 포트 설정 방법) (0) | 2015.11.06 |