#분류
찾아본 이유
Context envCtx = (Context) initCtx.lookup("java:comp/env"); //1. java:comp/env의 역할이 궁금해서 // Look up our data source DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); 2. lookup이 뭘 뭘 찾는지 궁금해서.
1. All configured entries and resources are placed in the java:comp/env
portion of the JNDI namespace(모든 엔트리와 리소스는 java:comp/env에 배치된다.)
2. lookup("jdbc/EmployeeDB")에서 jdbc/EmployeeDB는 우리가 생성한 리소스의 이름이다.
추가적으로 안 사실
1. 구현 방법에는 web.xml에 기술하는 방법이 있고 context.xml에 기술하는 방법이 있다.
#분류
context.xml configuration(설정)
If Tomcat is unable to identify the appropriate resource factory and/or additional configuration information is required, additional Tomcat specific configuration must be specified before Tomcat can create the resource. Tomcat specific resource configuration is entered in the <Context>
elements that can be specified in either $CATALINA_BASE/conf/server.xml
or, preferably, the per-web-application context XML file (META-INF/context.xml
).
Tomcat specific resource configuration is performed using the following elements in the <Context>
element:
InitialContext
(equivalent to the inclusion of an <env-entry>
element in the web application deployment descriptor).<resource-ref>
element in the web application deployment descriptor).java:comp/UserTransaction
.Any number of these elements may be nested inside a <Context>
element and will be associated only with that particular web application.
If a resource has been defined in a <Context>
element, it is not necessary for that resource to be defined in /WEB-INF/web.xml
. However, it is recommended to keep the entry in /WEB-INF/web.xml
to document the resource requirements for the web application.
Where the same resource name has been defined for a <env-entry>
element included in the web application deployment descriptor (/WEB-INF/web.xml
) and in an <Environment>
element as part of the <Context>
element for the web application, the values in the deployment descriptor will take precedence only if allowed by the corresponding <Environment>
element (by setting the override
attribute to "true").
#분류
Using resources
The InitialContext
is configured as a web application is initially deployed, and is made available to web application components (for read-only access). All configured entries and resources are placed in the java:comp/env
portion of the JNDI namespace(모든 엔트리와 리소스는 java:comp/env에 배치된다.), so a typical access to a resource - in this case, to a JDBC DataSource
- would look something like this:
// Obtain our environment naming context Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); // Look up our data source DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); // Allocate and use a connection from the pool Connection conn = ds.getConnection(); ... use this connection to access the database ... conn.close(); | ||
#분류
JDBC Data Sources
Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both Tomcat internal classes and to your web application. This is most easily accomplished by installing the driver's JAR file(s) into the $CATALINA_HOME/lib
directory, which makes the driver available both to the resource factory and to your application.
Next, modify the web application deployment descriptor (/WEB-INF/web.xml
) to declare the JNDI name under which you will look up preconfigured data source. By convention, all such names should resolve to the jdbc
subcontext (relative to the standard java:comp/env
naming context that is the root of all provided resource factories. A typical web.xml
entry might look like this:
<resource-ref> <description> Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular database that is configured in the <Context> configurartion for the web application. </description> <res-ref-name> jdbc/EmployeeDB </res-ref-name> <res-type> javax.sql.DataSource </res-type> <res-auth> Container </res-auth> </resource-ref> | ||
WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details.
A typical use of this resource reference might look like this:
Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); Connection conn = ds.getConnection(); ... use this connection to access the database ... conn.close(); | ||
Note that the application uses the same resource reference name that was declared in the web application deployment descriptor. This is matched up against the resource factory that is configured in the <Context>
element for the web application as described below.
To configure Tomcat's resource factory, add an element like this to the <Context>
element for the web application.
<Context ...>
...
<Resource name="jdbc/EmployeeDB"
auth="Container"
type="javax.sql.DataSource"
username="dbusername"
password="dbpassword"
driverClassName="org.hsql.jdbcDriver"
url="jdbc:HypersonicSQL:database"
maxActive="8"
maxIdle="4"/>
...
</Context>
| ||
Note that the resource name (here, jdbc/EmployeeDB
) must match the value specified in the web application deployment descriptor.
출처 - http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html#Tomcat_Standard_Resource_Factories
JK플립플롭의 특성표와 여기표와 상태표의 관계 (0) | 2018.02.20 |
---|---|
자바의 출시 후의 기여와 거기서 발생한 이슈 (0) | 2018.02.19 |
헷갈리는 WEB과 WAS (0) | 2018.02.09 |
Router 명령어 (0) | 2017.11.24 |
정보처리기사 - 정규화 (0) | 2017.11.23 |