如何在context.xml中存储字符串值
我想将连接URL存储在我的Tomcat应用程序的JNDI绑定中。 由于Tomcat使用context.xml
进行JNDI资源定义,因此我需要弄清楚在context.xml
存储String(或多个连接的多个字符串)的context.xml
。
我这样做的原因是我可以为不同的环境定义不同的字符串,并通过JNDI加载它们。
通常,我看到这样的条目:
这真的很简单:
如果是这样, 我在哪里实际存储String值?!?! 如果它不正确,那么我在context.xml
存储的正确方法是什么,例如“ amqp:5272//blah.example.com¶m1=4
”,这样我就可以这样查找:
Context ctx = new InitialContext(); String connectionURL = (String)ctx.lookup("myConnectionURL");
提前致谢!
您可以通过在此元素中嵌套元素来配置将作为servlet上下文初始化参数对Web应用程序可见的命名值。 例如,您可以创建如下的初始化参数:
... ... This is equivalent to the inclusion of the following element in the web application deployment descriptor (/WEB-INF/web.xml): companyName My Company, Incorporated
你的java代码看起来像这样
ServletContext sc = getServletContext(); String companyName = sc.getInitParameter("companyName");
请参阅参考http://tomcat.apache.org/tomcat-7.0-doc/config/context.html
您可以使用Environment
标记:
您几乎可以像在问题中指定的那样阅读它:
InitialContext initialContext = new InitialContext(); Context environmentContext = (Context) initialContext.lookup("java:/comp/env"); String connectionURL = (String) environmentContext.lookup("myConnectionURL");
这与使用Parameter
标签非常相似,但不需要ServletContext
。