jetty 7.x 에서 JNDI Oracle Datasource 설정하기
쓸만한지식 2014. 2. 18. 16:35
먼저 Datasource 를 설정하기 위해 당연히 WEB-INF/lib 에 오라클 jdbc 드라이버 jar 파일이 들어가 있어야 된다.
JNDI 설정하기
1. WEB-INF/jetty-web.xml 파일 추가
jetty-web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <? xml version = "1.0" ?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> < Configure class = "org.eclipse.jetty.webapp.WebAppContext" > < New class = "org.eclipse.jetty.plus.jndi.Resource" > < Arg ></ Arg > < Arg >JNDI 이름</ Arg > < Arg > < New class = "oracle.jdbc.pool.OracleConnectionPoolDataSource" > < Set name = "URL" >jdbc:oracle:thin:@아이피:포트:디비이름</ Set > < Set name = "User" >아이디</ Set > < Set name = "Password" >비밀번호</ Set > </ New > </ Arg > </ New > </ Configure > |
이것저것 찾아보니 요렇게 설정한 다음에 web.xml 파일에 <resource-ref> 부분을 추가해 주라고 하던데 테스트 해보니 굳이 추가 안해줘도 된다.
2. JSP 에서 쪽바로 되는지 테스트
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <% @page import = "java.sql.ResultSet" %> <% @page import = "java.sql.Connection" %> <% @page import = "javax.sql.DataSource" %> <% @page import = "javax.naming.InitialContext" %> <% @page import = "javax.naming.Context" %> <% Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup( "JNDI 이름" ); // 이름을 myds 로 설정 했다면 ctx.lookup("myds"); 요렇게 // 이름을 jdbc/myds 로 설정 했다면 ctx.lookup("jdbc/myds"); 요렇게 lookup 하면된다. Connection conn = ds.getConnection(); ResultSet rs = conn.prepareStatement( "select sysdate from dual" ).executeQuery(); while ( rs.next() ){ out.print(rs.getString( "sysdate" )); } %> |
※ JNDI 트리 확인해 보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.tistory.stove99; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NameClassPair; import javax.naming.NamingEnumeration; import javax.naming.NamingException; public class JNDIUtil { public final static boolean INCREASE = true ; public final static boolean DECREASE = false ; private int level = 0 ; private Context context = null ; public JNDIUtil() throws NamingException{ context = new InitialContext(); } public void print(){ print( "" ); } public void print(String name){ try { print(context.list(name), name); } catch (NamingException e){} } private void print(NamingEnumeration pairs, String parent) throws NamingException{ while (pairs.hasMoreElements()) { NameClassPair pair = (NameClassPair)pairs.nextElement(); print(pair); indent(JNDIUtil.INCREASE); print((parent.length() == 0 ) ? pair.getName() : parent + "/" + pair.getName()); indent(JNDIUtil.DECREASE); } } private void print(NameClassPair entry){ for ( int i = 0 ; i < level; i++) { System.out.print( " " ); } System.out.println( "-->" + entry); } private void indent( boolean increase){ if (increase) level += 4 ; else level -= 4 ; } } |
JSP 쪽에서 요 클래스를 땡겨다 실행해 보면 콘솔창에서 JNDI 구조를 확인해 볼 수 있다.
1 2 3 4 | <% @page import = "com.tistory.stove99.JNDIUtil" %> <% new JNDIUtil().print(); %> |
'쓸만한지식' 카테고리의 다른 글
Ubuntu GitLab 설치하기 with apache, mysql (0) | 2014.03.27 |
---|---|
Linux 서버에서 java.lang.NoClassDefFoundError: sun/awt/X11GraphicsEnvironment 에러날때 (1) | 2014.03.18 |
java.lang.NoSuchMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext; 에러날때 (0) | 2014.02.14 |
Eclipse juno 툴바에 안드로이드 관련 버튼이 없을때 (2) | 2013.06.20 |
Windows 8 에서 Android SDK Manager 및 AVD Manager 가 실행이 안될때 (5) | 2013.06.20 |