简体   繁体   中英

Tomcat: context.xml and web.xml configs for jndi resource

I have been going over almost all the web.xml/context.xml related questions over the web but nowhere did i get a clear enough answer to my following question. This says that the resource-ref tag in web.xml is equivalent to Resource tag in context.xml. This says that resource-ref tag in web.xml looks up the Resource tag in context.xml. Now these two sound confusing specifically because both the links I have referred to are Tomcat doc links and still have seemingly contradictory statements. Any clarifications would be extremely helpful.

The "Resource" tag defines the resource and can be placed in a number of xml files that largely depend upon the deployment preferences. To get started I'd place a context.xml in the META-INF folder in the web app. This directory is at the same level in the web app as the WEB-INF folder an example being:-

META-INF/context.xml

<Context>
   <Resource name="jdbc/TestDB" auth="Container"  
              type="javax.sql.DataSource"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/testdb"
              username="dbuser"
              password="dbpassword"
              maxTotal="20" maxIdle="10" maxWaitMillis="-1"/>
</Context>

The above makes the resource available.

The resource-ref tag is used to refer to the resource to make it avaiable to your app. This can go in the web.xml file.

WEB-INF/web.xml

<web-app>
    <!--- snipped -->
    <resource-ref>
        <description>Test DB Connection</description>
        <res-ref-name>jdbc/TestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

Both of these can be configured in other ways. I usually go with something like this then dabble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM