Search code examples
javatomcattomcat9

Context value not being read


I have the following entry in my context.xml:

<Environment name="environment" value="DEV" type="java.lang.String" override="false" />

And a servlet that calls for that -

        try
        {
            Context initContext = new InitialContext();
            environment = (String) initContext.lookup("environment");
        }
        catch (final NamingException nx)
        {
            ERROR_RECORDER.error(nx.getMessage(), nx);
        }

Now, the code works. I can run it in other containers and it's fine. This is only happening under Tomcat 9.0.73, and according to the Tomcat docs I have this configured correctly, but I'm getting the following exception:

[Time: 16 Mar 2023 06:36:36,840][Thread: main][Log: ERROR_RECORDER.][Level: ERROR] - [File: ResponseTimeFilter.java:80] - Name [environment] is not bound in this Context. Unable to find [environment].
javax.naming.NameNotFoundException: Name [environment] is not bound in this Context. Unable to find [environment].

Anyone have any ideas?


Solution

  • I found the problem... I didn't have an

    Context envContext = (Context) initContext.lookup("java:comp/env");
    

    It would seem that IBM WebSphere Application Server (our primary application server) adds this information for you...

    my mistake.