How to store string values in context.xml
Asked Answered
C

2

24

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.

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>

Is it really just as simple as:

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

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");

Thanks in advance!

Coexist answered 23/8, 2013 at 13:34 Comment(0)
R
11

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>

Your java code looks like this

 ServletContext sc = getServletContext();  

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

Please see the reference http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

Rambouillet answered 23/8, 2013 at 13:45 Comment(1)
this does not answer the question asked even though it is marked as the answer and may have helped the original question submitter. For those of you coming to this post looking for a solution for the posted question, refer to the answer provided by @CorreaBirdt
C
31

You can use an Environmenttag:

<Context>
    <Environment name="myConnectionURL" value="amqp:5272//blah.example.com&param1=4" type="java.lang.String"/>
</Context>

And you can read it almost as you specified in the question:

InitialContext initialContext = new InitialContext();
Context environmentContext = (Context) initialContext.lookup("java:/comp/env");
String connectionURL = (String) environmentContext.lookup("myConnectionURL");

This is much the same as using a Parameter tag, but without the need for a ServletContext.

Correa answered 9/6, 2014 at 14:18 Comment(2)
Should the file name be context.xml and where is it stored?Tributary
@gkiko: in Tomcat it's called context.xml and there should already be one in the conf directory of your tomcat installation, on *nix usually at /usr/share/tomcat/conf/context.xml. Other web servers have other files and conventions.Correa
R
11

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>

Your java code looks like this

 ServletContext sc = getServletContext();  

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

Please see the reference http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

Rambouillet answered 23/8, 2013 at 13:45 Comment(1)
this does not answer the question asked even though it is marked as the answer and may have helped the original question submitter. For those of you coming to this post looking for a solution for the posted question, refer to the answer provided by @CorreaBirdt

© 2022 - 2024 — McMap. All rights reserved.