On Tue, 21 Jan 2003, Shapira, Yoav wrote:

> Date: Tue, 21 Jan 2003 15:45:10 -0500
> From: "Shapira, Yoav" <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: Tomcat Users List <[EMAIL PROTECTED]>
> Subject: RE: config.getInitParameters
>
> Hi,
> This requirement is very common.  The way I like to do it is by having
> one web.xml with an ant token for the value of the parameter.  Then the
> various deploy targets in our ant scripts copy the web.xml with
> filtering to fill in the token value.  We have properties files for our
> different environments.
>
> So it looks like this:
>
> web.xml:
> <init-param>
>   <param-name>dbUrl</param-name>
>   <param-value>@dbUrlToken@</param-value>
> </init-param>
>
> Ant script:
> <filter token="dbUrlToken" value="jdbc:oracle:..." />
> <copy to="..." src="..." filtering="true">
>   <fileset ... includes="web.xml" />
> </copy>
>
> I hope the above is enough to explain the idea,
>

Another approach is to use the JNDI naming context facility that is there
for exactly this kind of purpose -- environment entries.  The actual value
to use is configured in server.xml, and can be retrieved by your app
without any modification to the contents of the webapp.

(1) Declare in your web.xml that you want to use an
    environment entry:

    <env-entry>
      <description>
        The connection URL for my database.
      </description>
      <env-entry-name>dbURL</env-entry-name>
      <!-- you can optionally declare a default value
           in an <env-entry-value> element here -->
      <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>

(2) Access and use the environment entry value in your servlet:

    InitialContext ic = new InitialContext();
    String dbURL = (String) ic.lookup("java:comp/env/dbURL");

(3) Configure the value in your server.xml file:

    <Context path="/myapp" ...>
      ...
      <Environment name="dbURL" type="java.lang.String"
                  value="...the actual URL value..."/>
      ...
    </Context>

Now, you can deploy exactly the same webapp on different servers, and have
them contact different databases, without having to modify anything inside
the webapp itself.

Except for step (3), which is Tomcat specific (every container will
provide their own way to configure these kinds of things), the use of
environment entries is portable to any J2EE app server.


> Yoav Shapira
> Millennium ChemInformatics
>

Craig




--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to