问题描述
如何查找:
Context envContext = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/MyDatasource");
继续?
我的意思是说如何搜索名称 MyDataSource
并最终返回什么?
I mean to say how is the name MyDataSource
searched and in the end what is returned ?
添加了两个连接数据库的条目。一个在 WEB-INF / web.xml
中,它是:
There are two entries added to connect to the database. One in the WEB-INF/web.xml
which is :
<resource-ref>
<description>my connection</description>
<res-ref-name>jdbc/MyDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
和另一个添加在 META-INF / context.xml
这是:
<Resource name="jdbc/MyDatasource" auth="Container" type="javax.sql.DataSource"
driverClassName="org.apache.derby.jdbc.ClientDriver"
url="jdbc:derby://localhost:1527/My Database;create=true"
username="me" password="me"
maxActive="20" maxIdle="10" maxWait="-1" />
这2个条目如何帮助查找?
How does these 2 entries help in the look up ?
首先看一下: web.xml
或 context.xml
?
请在查询中解释整个过程。
Please explain the whole process in the look up.
推荐答案
资源按以下优先顺序排列: web.xml
(通过< resource-ref>
元素, context.xml
, server.xml
(通过< GlobalNamingResources>
请注意,< Context>
中定义的资源实际上并不需要具有相应的< resource-ref>
web.xml
中的元素。请参阅有关JNDI资源的Tomcat文档:
Resources are located in this order of preference: web.xml
(via <resource-ref>
elements, context.xml
, server.xml
(via <GlobalNamingResources>
). Note that resource defined in your <Context>
do not actually need to have corresponding <resource-ref>
elements in your web.xml
. See the Tomcat documentation regarding JNDI resources: http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
这篇关于查找中遵循的步骤是什么?首先看看web.xml或context.xml是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!