查找中遵循的步骤是什么?什么是首先看web.xml或context.xml?

时间:2012-07-19 04:18:47

标签: java java-ee tomcat web.xml context.xml

如何查找:

Context envContext = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/MyDatasource");

继续?

我的意思是说如何搜索名称 MyDataSource并最终返回什么内容?

添加了两个条目以连接到数据库。 WEB-INF/web.xml中的一个是:

<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个条目如何帮助查找?

先看了什么:web.xmlcontext.xml

请在查询中解释整个过程。

2 个答案:

答案 0 :(得分:1)

资源位于以下偏好顺序:web.xml(通过<resource-ref>元素,context.xmlserver.xml(通过<GlobalNamingResources>)。请注意资源已定义您的<Context>中实际上不需要在<resource-ref>中包含相应的web.xml元素。请参阅有关JNDI资源的Tomcat文档:http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html

答案 1 :(得分:0)

以下是执行查找的步骤:
第1步:

 Context context = new InitialContext(): 

初始上下文是对JNDI查找服务的引用。 它就像进入JNDI虚拟目录树。

第2步:

Object o = context.lookup("mejb"): 

在查找中,我们需要给出服务器中部署的bean的名称,以获得对该bean的home接口的引用。然后我们得到java.lang.Object类型的对象将此对象转换为我们查找的任何bean的Home接口。

第3步:

Home home = (Home) PortableRemoteObject.narrow(o,Home.class):

我们实际上需要将对象转换为我们认为它是类型的类型。但是,由于这是基于IIOP的RMI,我们似乎需要使用PortableRemoteObject.narrow方法,因为它似乎将对象类型过滤为实际的对象类型并检查错误。

相关问题