如何在context.xml中存储字符串值 [英] How to store string values in context.xml

查看:193
本文介绍了如何在context.xml中存储字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将连接URL存储在我的Tomcat应用程序的JNDI绑定中。由于Tomcat使用 context.xml 进行JNDI资源定义,我需要找出在 context.xml 。

I'd like to store connection URLs in a JNDI binding for my Tomcat application. Since Tomcat uses context.xml for JNDI resource defining, I need to figure out the propert way to store a String (or multiple strings for multiple connections) in context.xml.

我这样做的原因是我可以为不同的环境定义不同的字符串,并通过JNDI加载它们。

My reason for doing this is so that I can define different strings for different environments, and load them through JNDI.

通常情况下,我会看到如下条目:

Usually, I see entries like so:

<Context ...>
    <Resource name="someName" auth="Container"
            type="someFullyQualifiedClassName"
            description="Some description."/>
</Context>

这真的很简单:

<Context ...>
    <Resource name="myConnectionURL" auth="Container"
            type="java.lang.String"
            description="A connection URL string."/>
</Context>

如果是这样,我在哪里存储String值?!?!如果它不正确,那么我存储的正确方法是什么,例如, amqp:5272 // blah.example.com& param1 = 4 context.xml 中,我可以这样查看:

If so, where do I actually store the String value?!?! And if it's not correct, then what is the proper way for me to store, for instance, "amqp:5272//blah.example.com&param1=4" in context.xml so I could then look it up like so:

Context ctx = new InitialContext();
String connectionURL = (String)ctx.lookup("myConnectionURL");

提前致谢!

推荐答案

您可以通过在此元素中嵌套元素来配置将作为servlet上下文初始化参数对Web应用程序可见的命名值。例如,您可以创建如下的初始化参数:

You can configure named values that will be made visible to the web application as servlet context initialization parameters by nesting elements inside this element. For example, you can create an initialization parameter like this:

 <Context>
      ...
     <Parameter name="companyName" value="My Company, Incorporated"
          override="false"/>
       ...
 </Context>

   This is equivalent to the inclusion of the following element in the web application deployment descriptor (/WEB-INF/web.xml):



 <context-param>
       <param-name>companyName</param-name>
       <param-value>My Company, Incorporated</param-value>
 </context-param>

您的java代码如下所示

Your java code looks like this

 ServletContext sc = getServletContext();  

 String companyName = sc.getInitParameter("companyName");  

请参阅参考资料 http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

这篇关于如何在context.xml中存储字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆