DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=30079>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=30079

Inconsistent JNDI lookups within ServletContextListener

           Summary: Inconsistent JNDI lookups within ServletContextListener
           Product: Tomcat 5
           Version: 5.0.19
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: Major
          Priority: Other
         Component: Catalina
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


Hi.

I see some inconsistent behavior when using JNDI lookups within a
ServletContextListener. 
I join minimal sample files with which this issue can be reproduced.
I use tomcat 5.0.19 and sun's j2sdk 1.4.2_04, on linux.

When i try to read an <Environment> entry defined in the server.xml from my
application, I have the following results:

Starting from scratch a new web module, I add a DefaultContext, nested in the
appropriate Engine, in order to define my environment value in the server.xml,
like this:

===== server.xml ===== (this is not the complete file, I write engine and
service lines to show where I defined the DefaultContext)
  <Service name="Catalina">
    <Engine defaultHost="localhost" name="Catalina">

      <DefaultContext reloadable="true" debug="99">
         <Environment name="testEnvironment" value="value in server.xml"
type="java.lang.String"/>
      </DefaultContext>

    </Engine>
  </Service>
========


Then I call this jsp file to read my "testEnvironment"

===== index.jsp =====
<[EMAIL PROTECTED] contentType="text/html"%>
<[EMAIL PROTECTED] pageEncoding="UTF-8"%>
<html>
<head><title>JSP Page</title></head>
<body>
<%
    javax.naming.Context initCtx = new javax.naming.InitialContext();
    javax.naming.Context envCtx = (javax.naming.Context)
initCtx.lookup("java:comp/env");
%>

<%=envCtx.lookup("testEnvironment")%>
</body>
</html>
========

at this point, it works, and I can read "value in server.xml" in my browser.

The problem is that I need this value when the application starts. So I add the
following ServletContextListener:

===== TestListener.java =====
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.naming.*;

public class TestListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent e) {
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");

            e.getServletContext().log("Found environment entry: " +
envCtx.lookup("testEnvironment"));
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void contextDestroyed(ServletContextEvent e) {
    }
}
======

and declare this listener in my web.xml:

===== web.xml =====
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd";>
  <listener>
    <listener-class>TestListener</listener-class>
  </listener>
</web-app>
======

But now, when I start the application, I get the following exception (first few
lines) (in english, the first line should be something like "testEnvironment is
not bound to this context")

javax.naming.NameNotFoundException: Le Nom testEnvironment n'est pas lié à ce
Contexte
        at org.apache.naming.NamingContext.lookup(NamingContext.java:815)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:198)
        at TestListener.contextInitialized(TestListener.java:15)
        at
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3773)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4270)
        at
org.apache.catalina.core.StandardContext.reload(StandardContext.java:2990)
        at
org.apache.catalina.manager.ManagerServlet.reload(ManagerServlet.java:1019)


If I add the following entry in my web.xml, I don't get an exception anymore,
but the listener logs the value in web.xml while the jsp displays the value in
server.xml.
  <env-entry>
    <env-entry-name>testEnvironment</env-entry-name>
    <env-entry-value>value in web.xml</env-entry-value>
    <env-entry-type>java.lang.String</env-entry-type>
  </env-entry>


Then, if I remove the environment entry from server.xml, of course, both jsp and
listener get the value from web.xml.


So, from my point of view, it seems that tomcat calls the contextInitialized
method after it has read the web.xml but before it has completely initialized
the context with DefaultContext definitions from server.xml.

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

Reply via email to