java:comp/env is not bound

2019-07-24 21:59发布

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条回答
看我几分像从前
2楼-- · 2019-07-24 22:30

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)

查看更多
登录 后发表回答