J2EE SPEC specifies that every resource referenced by an application must be listed in web.xml file. The purpose of this regulation is to declare and externalize the dependencies of the application and make it is possible to change these dependencies during deployment without any code change.
However, changing dependencies this way may need some effort. Usually, web applications are packaged as .war files. Changing web.xml file inside a war file means repackaging the .war file.
Tomcat comes up with a better way to do the same thing - change dependencies without touching web.xml. The .war file remains intact.
Configuration element <context> can include <Environment>, <Resource>, <ResourceLink> and <Transaction> sub elements to re-define/define the values of the dependencies of the application deployed at this context.
These values defined in tomcat <context> element can override the corresponding values (with same names) defined in web.xml.
If the application doesn‘t list its dependencies using <env-entry>, <resource-ref> or <resource-env-ref> in web.xml (this is bad practice), it can also be deployed and run as long as the needed resources have been defined in context using above elements.
PS, if you want to use a global resource defined in server.xml, you should use <ResourceLink> instead of <Resource> in <Context>.
联系客服