Tomcat设置JNDI 资源


Web应用描述符文件(/WEB-INF/web.xml)可以用于设置资源的创建和引用。
设置时用到的元素有三个:
l        <env-entry>
l        <resource-ref>
l        <resource-env-ref>
如果Tomcat不能识别适当的资源工厂,或者还需要设置其他信息。必须在Tomcat创建资源前设置必要的信息,这些信息一般是在$CATALINA_BASE/conf/server.xml文件中设置的,最好是在每个Web应用的上下文设置文件(META-INF/context.xml)中设置。设置时用到的元素有:
l        <Environment>
l        <Resource>
l        <ResourceLink>
l        <Transaction>
在context.xml中定义的资源,没有必要在web.xml中定义。建议保留在web.xml中的定义是为了说明web应用对资源的要求。
如果在context.xml和web.xml中定义了相同名称的资源,只有在context.xml中设置了override属性为true,web.xml中的设置就可以优先。
全局设置
$CATALINA_BASE/conf/server.xml中的元素<GlobalNamingResources>是对服务器的全局资源的设置。在Web应用的context.xml中用<ResourceLink>可以包含这些资源。这样的资源,也没有必要在web.xml中定义,建议保留在web.xml中的定义是为了说明web应用对资源的要求。
web.xml文件示例:
<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>
 
 
context.xml文件示例:
<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>

相关内容

    暂无相关文章