java:comp/env is not bound

天涯浪子 提交于 2019-12-11 12:38:15

问题


I currently have a few libraries in my $CATALINA_HOME/lib directory to handle some system/DB authentication. One of those files foo.jar contains a class that extends

org.apache.catalina.realm.DataSourceRealm

and is being used to authentication. This file needs to access a few properties that I'm storing in server.xml However, when attempting to access it I'm getting an error. The segment of code is as follows

try{
Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/env");

String property = (String)envCtx.lookup("property");

} catch(Exception ex) {} 

Any ideas?


回答1:


You also need to have the associated ResourceLink in the context.xml for your app. For example, if your webapp is named foo, there should be a file called conf/Catalina/localhost/foo.xml with a

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE Context>

<Context antiJARLocking="true">
    <ResourceLink name="property" 
        type="classname" 
        global="propertyNameInGlobalNamingResources" />  
    ...
</Context>

If you include a META-INF/context.xml with these links in your war file, tomcat will copy it out to the conf/Catalina/localhost/foo.xml the first time the app is deployed (though not any time after that as you are intended to make changes there and be able to deploy again without clobbering customization)



来源:https://stackoverflow.com/questions/14966688/javacomp-env-is-not-bound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!